package com.jdy.haoduoaiteacher.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.SynthesizerListener;
import com.jdy.haoduoaiteacher.MyTeacherApp;
import com.jdy.haoduoaiteacher.adapter.CustomClassGridViewAdapter;
import com.jdy.haoduoaiteacher.adapter.CustomHabitGridViewAdapter;
import com.jdy.haoduoaiteacher.adapter.RadioCategoryGridViewAdapter;
import com.jdy.haoduoaiteacher.audioconverter.AndroidAudioConverter;
import com.jdy.haoduoaiteacher.audioconverter.callback.IConvertCallback;
import com.jdy.haoduoaiteacher.audioconverter.model.AudioFormat;
import com.jdy.haoduoaiteacher.intface.IAsyncTask;
import com.jdy.haoduoaiteacher.model.AlbumItem;
import com.jdy.haoduoaiteacher.model.CategoryItem;
import com.jdy.haoduoaiteacher.model.CustomAlbumItem;
import com.jdy.haoduoaiteacher.model.CustomHabitItem;
import com.jdy.haoduoaiteacher.model.LeHotDynamicItem;
import com.jdy.haoduoaiteacher.model.LeUser;
import com.jdy.haoduoaiteacher.model.PainPointItem;
import com.jdy.haoduoaiteacher.model.RadioCategoryItem;
import com.jdy.haoduoaiteacher.parse.JsonSerializer;
import com.jdy.haoduoaiteacher.util.ACache;
import com.jdy.haoduoaiteacher.util.CustomAsyncTask;
import com.jdy.haoduoaiteacher.util.HttpUtils;
import com.jdy.haoduoaiteacher.util.ResponseResult;
import com.jdy.haoduoaiteacher.util.Tools;
import com.jdy.haoduoaiteacher.view.MyGridView;
import com.jdy.haoduoaiteacher.view.VisualizerView;
import com.jdy.haoduoaiteacher.widget.CustomEditDialog;
import com.jdy.haoduoaiteacher.widget.CustomTimeDialog;
import com.jdy.haoduoaiteacher.widget.CustomTimeDialogDate;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.R;

import org.droidparts.annotation.inject.InjectView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static com.jdy.haoduoaiteacher.activity.AddHabitClockActivity.SelectedEnum.CommonClass;
import static com.jdy.haoduoaiteacher.activity.AddHabitClockActivity.SelectedEnum.CustomClass;
import static com.jdy.haoduoaiteacher.activity.AddHabitClockActivity.SelectedEnum.CustomContent;
import static com.jdy.haoduoaiteacher.activity.AddHabitClockActivity.SelectedEnum.CustomHabit;
import static com.jdy.haoduoaiteacher.activity.AddHabitClockActivity.SelectedEnum.Recording;


/**
 * Created by Administrator on 2017/4/12 00.
 * 教师端--添加课程界面
 */

public class AddHabitClockActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = AddHabitClockActivity.class.getSimpleName();

    @InjectView(id = R.id.custom_channel)
    private LinearLayout custom_channel;

    private RadioCategoryGridViewAdapter mRCGVAdapter;

    public enum SelectedEnum {
        None,
        CustomContent,
        Recording,
        CommonClass,
        CustomClass,
        CustomHabit  // 习惯定制
    }

    private SelectedEnum mCurrentSelectedEnum = CustomContent;


    @InjectView(id = R.id.selected_img)
    private ImageView mSelectedImg;
    @InjectView(id = R.id.img_emptystory)
    private ImageView img_emptystory;

    @InjectView(id = R.id.custom_class_btn, click = true)
    private Button custom_class_btn; // 课程课件
    @InjectView(id = R.id.common_class_btn, click = true)
    private Button common_class_btn; // 大众课程
    @InjectView(id = R.id.notify_btn, click = true)
    private Button notify_btn;  // 通知提醒

    @InjectView(id = R.id.customhabit_btn, click = true)
    private Button customhabit_btn;


    @InjectView(id = R.id.lin_trylisten, click = true)
    private LinearLayout lin_trylisten;
    @InjectView(id = R.id.tts_content_et)
    private EditText mTtsContentEt;
    @InjectView(id = R.id.save_and_upload_btn, click = true)
    private Button mSaveUploadBtn;
    @InjectView(id = R.id.record_state_btn, click = true)
    private Button mRecordStateBtn;
    @InjectView(id = R.id.retry_record_tv, click = true)
    private TextView mRetryTv;
    @InjectView(id = R.id.title_et)
    private EditText mTitleEt;
    @InjectView(id = R.id.set_timer_img, click = true)
    private ImageView mSetTimerImg;
    @InjectView(id = R.id.set_timer_tv, click = true)
    private TextView mSetTimerTv;
    @InjectView(id = R.id.preview_listen_caption_tv, click = true)
    private TextView mPreviewListenTv;
    @InjectView(id = R.id.rel_abhs)
    private View rel_abhs;
    @InjectView(id = R.id.rel_yxkc, click = true)
    private View rel_yxkc;
    @InjectView(id = R.id.view_lllll)
    private View view_lllll;


    @InjectView(id = R.id.lin_abhs)
    private View lin_abhs;
    @InjectView(id = R.id.tv_cv1)
    private TextView tv_cv1;
    @InjectView(id = R.id.tv_cv2)
    private TextView tv_cv2;
    @InjectView(id = R.id.tv_cv3)
    private TextView tv_cv3;
    @InjectView(id = R.id.tv_cv4)
    private TextView tv_cv4;
    @InjectView(id = R.id.tv_cv5)
    private TextView tv_cv5;
    @InjectView(id = R.id.tv_cv6)
    private TextView tv_cv6;
    @InjectView(id = R.id.tv_cv7)
    private TextView tv_cv7;

    @InjectView(id = R.id.select_cbb, click = true)
    private CheckBox select_cbb;
    @InjectView(id = R.id.preview_record_img, click = true)
    private ImageView mPreviewImg;
    @InjectView(id = R.id.recording_tip_tv)
    private TextView mRecordingTipTv;

    private CustomTimeDialog timeDlg;
    private CustomTimeDialogDate dateDlg;
    private SpeechSynthesizer mTts;

    // 缓冲进度
    private int mPercentForBuffering = 0;
    // 播放进度
    private int mPercentForPlaying = 0;
    // 默认发音人
    private String voicer = "nannan";

    // 引擎类型
    private String mEngineType = SpeechConstant.TYPE_CLOUD;

    private Toast mToast;

    @InjectView(id = R.id.record_visualizer)
    private VisualizerView mRecordVisualizerView;
    @InjectView(id = R.id.rel_notify)
    private View rel_notify;
    @InjectView(id = R.id.tv_custom11111, click = true)
    private TextView tv_custom11111;
    @InjectView(id = R.id.tv_record1111, click = true)
    private TextView tv_record1111;

    private MediaRecorder mRecorder = null;
    private File audioDirTemp;
    private boolean isRecording = false;
    private Handler mHandler; // Handler for updating the visualizer

    public static final String DIRECTORY_NAME_TEMP = "AudioTemp";
    public static final int REPEAT_INTERVAL = 15;

    private String classNum;

    private enum RecordingState {
        PRE_RECORDING,
        RECORDING,
        STOP_RECORDING,
    }

    private RecordingState RECORING_STATE = RecordingState.PRE_RECORDING;
    private String RECORDING_AUDIO_PATH = "";
    private String TTS_AUDIO_PATH = "";
    private MediaPlayer mp3Player;
    private LeUser leUser;
    private boolean isEdit, tryListenOnline;
    private PainPointItem mPainPointItem;
    private String selectQueryDate;
    private AnimationDrawable mAnim;

    @InjectView(id = R.id.bottom_gridview)
    private MyGridView mbottom_gridview; // 大众课程
    @InjectView(id = R.id.customclass_gridview)
    private MyGridView customclass_gridview; // 课程课件

    @InjectView(id = R.id.customhabit_gridview)
    private MyGridView customhabit_gridview;  // 习惯养成

    private CustomClassGridViewAdapter mCustomClassAdapter;
    private CustomHabitGridViewAdapter mCustomHabitAdapter;
    private int mLastPage = 1;

    @InjectView(id = R.id.content_layout)
    private RelativeLayout mContentLayout;

    private ACache mCache;

    private List<RadioCategoryItem> mCustomClassList;
    private List<RadioCategoryItem> mRadioCategoryList;
    private List<CustomHabitItem> mCustomHabitList;

    private TextView mLastTv = null;
    private int mSelectedId = -1;
    private boolean mIsPreviewing = false;

    private void restoreContentLayoutHeight() {
        RelativeLayout.LayoutParams iParams = (RelativeLayout.LayoutParams) mContentLayout
                .getLayoutParams();
        iParams.height = Tools.getPixelByDip(this, 210);
//        iParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        mContentLayout.setLayoutParams(iParams);
    }

    private void parseJsonArray(String data) {
        try {
            JSONArray jsonArray = new JSONArray(data);
            int size = jsonArray.length();
            mRadioCategoryList.clear();
            for (int i = 0; i < size; i++) {
                JSONObject jo = (JSONObject) jsonArray.get(i);
                String name = jo.getString("name");
                if (name.equals("情景")) {
                    // TODO
                } else if (name.equals("年龄")) {
                    // TODO
                } else {
                    RadioCategoryItem rci = JsonSerializer.getInstance().deserialize(
                            jo.toString(), RadioCategoryItem.class);
                    mRadioCategoryList.add(rci);
                }
            }
            setRadioCategoryView();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void parseCustomJsonArray(String data) {
        try {
            JSONArray jsonArray = new JSONArray(data);
            int size = jsonArray.length();
            mCustomClassList.clear();
            for (int i = 0; i < size; i++) {
                JSONObject jo = (JSONObject) jsonArray.get(i);
                RadioCategoryItem rci = JsonSerializer.getInstance().deserialize(
                        jo.toString(), RadioCategoryItem.class);
                mCustomClassList.add(rci);
            }
            setCustomCategoryView();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    private void setCustomCategoryView() {
        mCustomClassAdapter = new CustomClassGridViewAdapter(this, mCustomClassList);
        customclass_gridview.setAdapter(mCustomClassAdapter);
        customclass_gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CategoryItem categoryItem = convert2CI(mCustomClassList.get(position));
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                intent.setClass(AddHabitClockActivity.this, CustomExpandListActivity.class); // 课程课件
                bundle.putSerializable("categoryitem", categoryItem);
                bundle.putBoolean("isEdit", isEdit);
                intent.putExtras(bundle);
                startActivityForResult(intent, 1001);
            }
        });
    }

    private void setCustomHabitGridView() {
        mCustomHabitAdapter = new CustomHabitGridViewAdapter(this, mCustomHabitList);
        customhabit_gridview.setAdapter(mCustomHabitAdapter);
        customhabit_gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CategoryItem categoryItem = convert2CI(mCustomHabitList.get(position));
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                intent.setClass(AddHabitClockActivity.this, ExpandListActivity.class);
                bundle.putSerializable("categoryitem", categoryItem);
                bundle.putBoolean("isEdit", isEdit);
                intent.putExtras(bundle);
                startActivityForResult(intent, 1002);
            }
        });
    }

    private void parseCustomHabitJsonArray(String data) {
        try {
            JSONArray jsonArray = new JSONArray(data);
            int size = jsonArray.length();
            mCustomHabitList.clear();
            for (int i = 0; i < size; i++) {
                JSONObject jo = (JSONObject) jsonArray.get(i);
                CustomHabitItem rci = JsonSerializer.getInstance().deserialize(
                        jo.toString(), CustomHabitItem.class);
                mCustomHabitList.add(rci);
            }
            setCustomHabitGridView();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 习惯养成
     */
    private void loadCustomHabitData() {
        new CustomAsyncTask(this, new IAsyncTask() {
            @Override
            public void onRecieveData(Activity activity, ResponseResult result) {
                if (result.isSuccess()) {
                    if (Tools.isNotEmpty(result.data)) {
                        parseCustomHabitJsonArray(result.data);
                    }
                }
            }

            @Override
            public ResponseResult doInbackground(Activity activity) {
                LeUser lu = MyTeacherApp.getInstance().getUserTeacher();
                if (lu == null) {
                    lu = Tools.restoreLeUser();
                }

                HashMap<String, Object> param_ = new HashMap<String, Object>();
                param_.put("sno", lu.sno);
                param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);

                return HttpUtils.startRequest(HttpUtils.API_URL
                                + "/api/2.0/catalog/habit_collection", param_,
                        HttpUtils.TAG_GET_REQUEST);

            }
        }).execute();
    }

    private void loadCustomClassData() {
        new CustomAsyncTask(this, new IAsyncTask() {
            @Override
            public void onRecieveData(Activity activity, ResponseResult result) {
                if (result.isSuccess()) {
                    if (Tools.isNotEmpty(result.data)) {
                        parseCustomJsonArray(result.data);
                    }
                }
            }

            @Override
            public ResponseResult doInbackground(Activity activity) {
                LeUser lu = MyTeacherApp.getInstance().getUserTeacher();
                if (lu == null) {
                    lu = Tools.restoreLeUser();
                }

                HashMap<String, Object> param_ = new HashMap<String, Object>();
                param_.put("sno", lu.sno);
                param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);

                return HttpUtils.startRequest(HttpUtils.API_URL
                                + "/api/2.0/catalog/teacher/collection", param_,
                        HttpUtils.TAG_GET_REQUEST);

            }
        }).execute();
    }

    private void loadCategoryData() {
        String data = mCache.getAsString("RadioStationActivityloadCategoryData");
        if (Tools.isNotEmpty(data)) {
            parseJsonArray(data);
        } else {
            new CustomAsyncTask(this, new IAsyncTask() {
                @Override
                public void onRecieveData(Activity activity, ResponseResult result) {
                    if (result.isSuccess()) {
                        if (Tools.isNotEmpty(result.data)) {
                            mCache.remove("RadioStationActivityloadCategoryData");
                            mCache.put("RadioStationActivityloadCategoryData", result.data,
                                    2 * ACache.TIME_DAY);

                            parseJsonArray(result.data);
                        }
                    }
                }

                @Override
                public ResponseResult doInbackground(Activity activity) {
                    LeUser lu = MyTeacherApp.getInstance().getUserTeacher();
                    if (lu == null) {
                        lu = Tools.restoreLeUser();
                    }

                    HashMap<String, Object> param_ = new HashMap<String, Object>();
                    param_.put("sno", lu.sno);
                    param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);

                    return HttpUtils.startRequest(HttpUtils.API_URL
                                    + "/api/2.0/catalog/radio/collection", param_,
                            HttpUtils.TAG_GET_REQUEST);

                }
            }).execute();
        }
    }

    private void setContentLayoutToWrapHeight() {
        RelativeLayout.LayoutParams iParams = (RelativeLayout.LayoutParams) mContentLayout
                .getLayoutParams();
        iParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        mContentLayout.setLayoutParams(iParams);
    }

    private void startAnimation(TextView tv) {
        if (tv != null) {
            AnimationDrawable ad = (AnimationDrawable) ContextCompat.getDrawable(this, R.drawable.preview_anim);
            tv.setCompoundDrawablesWithIntrinsicBounds(null, null, ad, null);
            ad.start();
        }
    }

    private void stopAnimation(TextView tv) {
        if (tv != null) {

            Drawable[] drawables = tv.getCompoundDrawables();

            Drawable rightD = drawables[2];

            if (rightD instanceof AnimationDrawable) {
                AnimationDrawable ad = (AnimationDrawable) rightD;

                if (ad != null && ad.isRunning()) {
                    ad.stop();
                }

                Drawable d = ContextCompat.getDrawable(this, R.drawable.preview_record_img);

                tv.setCompoundDrawablesWithIntrinsicBounds(null, null, d, null);

            } else {
                return;
            }
        }
    }


    private void startAnimation() {
        mPreviewImg.setImageResource(R.drawable.preview_anim);
        mAnim = (AnimationDrawable) mPreviewImg.getDrawable();
        mAnim.start();
    }

    private void stopAnimation() {
        if (mAnim != null && mAnim.isRunning()) {
            mAnim.stop();
        }
        mPreviewImg.setImageResource(R.drawable.preview_record_img);
    }

    private String mCurDay, mCurDay2, mCurDay3, mCurDay4, mCurDay5, mCurDay6, mCurDay7;

    private void stopAnimation2() {
        if (mAnim != null && mAnim.isRunning()) {
            mAnim.stop();
        }
        //mPreviewImg2.setImageResource(R.drawable.preview_record_img);

        mIsPreviewingStorySong = false;
    }

    private boolean selectABHS = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_habit_layout_teacher);
        SpeechUtility.createUtility(AddHabitClockActivity.this, "appid=" + "574e86a0");
        leUser = MyTeacherApp.getInstance().getUserTeacher();
        classNum = getIntent().getStringExtra("classnum");

        select_cbb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                selectABHS = isChecked;

                if (selectABHS && !isEdit) {
                    lin_abhs.setVisibility(View.VISIBLE);

                    tv_cv1.setText("1. " + mCurDay + "   " + mSetTimerTv.getText().toString());
                    tv_cv2.setText("2. " + mCurDay2 + "   " + mSetTimerTv.getText().toString());
                    tv_cv3.setText("3. " + mCurDay3 + "   " + mSetTimerTv.getText().toString());
                    tv_cv4.setText("4. " + mCurDay4 + "   " + mSetTimerTv.getText().toString());
                    tv_cv5.setText("5. " + mCurDay5 + "   " + mSetTimerTv.getText().toString());
                    tv_cv6.setText("6. " + mCurDay6 + "   " + mSetTimerTv.getText().toString());
                    tv_cv7.setText("7. " + mCurDay7 + "   " + mSetTimerTv.getText().toString());
                } else {
                    lin_abhs.setVisibility(View.GONE);
                }
            }
        });


        ////////////////////////////////////
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        mCurDay = sdf.format(Calendar.getInstance().getTime());


        Date date2 = Tools.getDateAfter(Calendar.getInstance().getTime(), 1);
        Date date3 = Tools.getDateAfter(Calendar.getInstance().getTime(), 3);
        Date date4 = Tools.getDateAfter(Calendar.getInstance().getTime(), 6);
        Date date5 = Tools.getDateAfter(Calendar.getInstance().getTime(), 13);
        Date date6 = Tools.getDateAfter(Calendar.getInstance().getTime(), 27);
        Date date7 = Tools.getDateAfter(Calendar.getInstance().getTime(), 59);

        mCurDay2 = sdf.format(date2);
        mCurDay3 = sdf.format(date3);
        mCurDay4 = sdf.format(date4);
        mCurDay5 = sdf.format(date5);
        mCurDay6 = sdf.format(date6);
        mCurDay7 = sdf.format(date7);

        ////////////////////////////////////


        this.titleLayout.setBackgroundColor(Color.argb(255, 255, 255, 255));
        this.mBackImageButton.setImageResource(R.drawable.black_back);
        this.mTvUpright.setText("完成");
        this.mTvUpright.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                publishCustomHabit();
            }
        });
        this.mTvUpright.setVisibility(View.VISIBLE);
        this.mTvUpright.setTextColor(Color.argb(255, 19, 19, 19));

        mCache = ACache.get(this);
        mRadioCategoryList = new ArrayList<RadioCategoryItem>();
        mCustomClassList = new ArrayList<RadioCategoryItem>();
        mCustomHabitList = new ArrayList<CustomHabitItem>();

        this.mTitleText.setTextColor(Color.argb(255, 19, 19, 19));
        this.mRetryTv.setPaintFlags(mRetryTv.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

        mTts = SpeechSynthesizer.createSynthesizer(AddHabitClockActivity.this, mTtsInitListener);

        mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);

        audioDirTemp = new File(Environment.getExternalStorageDirectory(),
                DIRECTORY_NAME_TEMP);
        if (audioDirTemp.exists()) {
            deleteFilesInDir(audioDirTemp);
        } else {
            audioDirTemp.mkdirs();
        }

        RECORDING_AUDIO_PATH = audioDirTemp + "/audio_file"
                + ".amr";

        TTS_AUDIO_PATH = Environment.getExternalStorageDirectory() + "/msc/tts.wav";

        File wavFile = new File(TTS_AUDIO_PATH);
        if (wavFile.exists()) {
            wavFile.delete();
        }

        File mp3File = new File(TTS_AUDIO_PATH.replace(".wav", ".mp3"));
        if (mp3File.exists()) {
            mp3File.delete();
        }

        // create the Handler for visualizer update
        handler = new Handler();

        InputFilter[] filters = new InputFilter[1];
        filters[0] = new InputFilter.LengthFilter(140); //Filter to 10 characters

        mTitleEt.setFilters(filters);


        InputFilter[] filters2 = new InputFilter[1];

        filters2[0] = new InputFilter.LengthFilter(140); //Filter to 140 characters

        mTtsContentEt.setFilters(filters2);

        mTtsContentEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 140)
                    showTip("最多不超过140个字.");
            }
        });

        mp3Player = new MediaPlayer();

        Intent it = getIntent();
        if (it != null) {
            isEdit = it.getBooleanExtra("isedit", false);
            mPainPointItem = (PainPointItem) it.getSerializableExtra("custompainpoint");
            selectQueryDate = it.getStringExtra("selectQueryDate");


            if (Tools.isEmpty(selectQueryDate) && mPainPointItem != null) {
                selectQueryDate = mPainPointItem.date_from;

                ctype = mPainPointItem.ctype;
                originalctype = ctype;
                info_img = mPainPointItem.info_img;
                info_title = mPainPointItem.info_title;
                info_desc = mPainPointItem.info_desc;
            }
        }

        if (isEdit && mPainPointItem != null) {
            mTitleEt.setText(mPainPointItem.name);
            int lastIdx = mPainPointItem.time.lastIndexOf(":");
            mSetTimerTv.setText(mPainPointItem.time.substring(0, lastIdx));
            this.mTitleText.setText("编辑课程");

            if (Tools.isNotEmpty(mPainPointItem.mp3_url)) {
                tryListenOnline = true;
                mp3url_Online = mPainPointItem.mp3_url;
            }

            if (ctype == 0) {
                mCurrentSelectedEnum = CustomClass;
                switchSelectState(mCurrentSelectedEnum);

                if (Tools.isNotEmpty(info_title)) {
                    custom_channel.setVisibility(View.VISIBLE);

                    if (mCurrentSelectedEnum == CommonClass || mCurrentSelectedEnum == CustomHabit) {
                        imgList1.clear();
                        custom_channel.removeAllViews();
                        //  for (int i = 0; i < aiList.size(); i++) {
                        View vvv = LayoutInflater.from(this).inflate(R.layout.select_class_item,
                                custom_channel, false);

                        custom_channel.addView(vvv);

                        TextView storynumber = (TextView)
                                vvv.findViewById(R.id.storynumber);
                        TextView storytittle = (TextView)
                                vvv.findViewById(R.id.storytittle);
                        ImageView img_2222 = (ImageView) vvv.findViewById(R.id.img_2222);

                        if (Tools.isNotEmpty(info_img)) {
                            Picasso.with(this).load(info_img).into(img_2222);
                        }
                        if (storynumber != null) {
                            storynumber.setText(info_title);
                            storytittle.setText(info_desc);
                        }


                        final ImageView preview_record_img2 = (ImageView) vvv.findViewById(R.id.preview_record_img2);
                        imgList1.add(preview_record_img2);

                        vvv.findViewById(R.id.lin_trylisten).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                stopAnimation2();

                                for (ImageView item : imgList1) {
                                    item.setImageResource(R.drawable.preview_record_img);
                                }

                                releaseMp3Player();

                                if (Tools.isNotEmpty(mPainPointItem.mp3_url)) {
                                    mIsPreviewingStorySong = true;
                                    if (mp3Player == null)
                                        mp3Player = new MediaPlayer();
                                    try {
                                        mp3Player.setDataSource(mPainPointItem.mp3_url);
                                        mp3Player.prepare();
                                        mp3Player.start();
                                        //  startAnimation();
                                        preview_record_img2.setImageResource(R.drawable.preview_anim);
                                        mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                        mAnim.start();

                                        mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                    } catch (IllegalArgumentException e) {
                                        e.printStackTrace();
                                    } catch (SecurityException e) {
                                        e.printStackTrace();
                                    } catch (IllegalStateException e) {
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                } else {
                                    mIsPreviewingStorySong = !mIsPreviewingStorySong;
                                    if (mIsPreviewingStorySong) {
                                        if (mp3Player == null)
                                            mp3Player = new MediaPlayer();
                                        try {
                                            mp3Player.setDataSource(mPainPointItem.mp3_url);
                                            mp3Player.prepare();
                                            mp3Player.start();

                                            preview_record_img2.setImageResource(R.drawable.preview_anim);
                                            mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                            mAnim.start();

                                            mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                        } catch (IllegalArgumentException e) {
                                            e.printStackTrace();
                                        } catch (SecurityException e) {
                                            e.printStackTrace();
                                        } catch (IllegalStateException e) {
                                            e.printStackTrace();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    } else {
                                        stopAnimation2();
                                        releaseMp3Player();

                                        preview_record_img2.setImageResource(R.drawable.preview_record_img);
                                    }
                                }
                            }
                        });
                        // }
                    } else if (mCurrentSelectedEnum == CustomClass) {
                        custom_channel.removeAllViews();
                        imgList2.clear();

                        View vvv = LayoutInflater.from(this).inflate(R.layout.select_class_item,
                                custom_channel, false);

                        custom_channel.addView(vvv);

                        TextView storynumber = (TextView)
                                vvv.findViewById(R.id.storynumber);
                        TextView storytittle = (TextView)
                                vvv.findViewById(R.id.storytittle);
                        TextView tv_storylist = (TextView)
                                vvv.findViewById(R.id.tv_storylist);
                        ImageView img_2222 = (ImageView) vvv.findViewById(R.id.img_2222);


                        if (Tools.isNotEmpty(info_img)) {
                            Picasso.with(this).load(info_img).into(img_2222);
                        }
                        if (storynumber != null) {
                            storynumber.setText(info_title);
                            storytittle.setText(info_desc);
                        }

                        final ImageView preview_record_img2 = (ImageView) vvv.findViewById(R.id.preview_record_img2);
                        imgList2.add(preview_record_img2);


                        vvv.findViewById(R.id.lin_trylisten).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                stopAnimation2();

                                for (ImageView item : imgList2) {
                                    item.setImageResource(R.drawable.preview_record_img);
                                }

                                releaseMp3Player();

                                if (Tools.isNotEmpty(mPainPointItem.mp3_url)) {
                                    mIsPreviewingStorySong = true;
                                    if (mp3Player == null)
                                        mp3Player = new MediaPlayer();
                                    try {
                                        mp3Player.setDataSource(mPainPointItem.mp3_url);
                                        mp3Player.prepare();
                                        mp3Player.start();
                                        //  startAnimation();
                                        preview_record_img2.setImageResource(R.drawable.preview_anim);
                                        mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                        mAnim.start();

                                        mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                    } catch (IllegalArgumentException e) {
                                        e.printStackTrace();
                                    } catch (SecurityException e) {
                                        e.printStackTrace();
                                    } catch (IllegalStateException e) {
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                } else {
                                    mIsPreviewingStorySong = !mIsPreviewingStorySong;
                                    if (mIsPreviewingStorySong) {
                                        if (mp3Player == null)
                                            mp3Player = new MediaPlayer();
                                        try {
                                            mp3Player.setDataSource(mPainPointItem.mp3_url);
                                            mp3Player.prepare();
                                            mp3Player.start();

                                            preview_record_img2.setImageResource(R.drawable.preview_anim);
                                            mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                            mAnim.start();

                                            mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                        } catch (IllegalArgumentException e) {
                                            e.printStackTrace();
                                        } catch (SecurityException e) {
                                            e.printStackTrace();
                                        } catch (IllegalStateException e) {
                                            e.printStackTrace();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    } else {
                                        stopAnimation2();
                                        releaseMp3Player();

                                        preview_record_img2.setImageResource(R.drawable.preview_record_img);
                                    }
                                }
                            }
                        });


                    }
                }

                ViewGroup.LayoutParams pp = mContentLayout.getLayoutParams();
                pp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                mContentLayout.setLayoutParams(pp);

                // rel_abhs.setVisibility(View.VISIBLE);
                if (mPainPointItem.emc == 1) {
                    selectABHS = true;
                    select_cbb.setChecked(true);
                } else {
                    selectABHS = false;
                    select_cbb.setChecked(false);
                }
            } else if (ctype == 1) {
                mCurrentSelectedEnum = CommonClass;
                switchSelectState(mCurrentSelectedEnum);

                if (Tools.isNotEmpty(info_title)) {
                    custom_channel.setVisibility(View.VISIBLE);

                    if (mCurrentSelectedEnum == CommonClass || mCurrentSelectedEnum == CustomHabit) {
                        imgList1.clear();
                        custom_channel.removeAllViews();
                        //  for (int i = 0; i < aiList.size(); i++) {
                        View vvv = LayoutInflater.from(this).inflate(R.layout.select_class_item,
                                custom_channel, false);

                        custom_channel.addView(vvv);

                        TextView storynumber = (TextView)
                                vvv.findViewById(R.id.storynumber);
                        TextView storytittle = (TextView)
                                vvv.findViewById(R.id.storytittle);
                        ImageView img_2222 = (ImageView) vvv.findViewById(R.id.img_2222);

                        if (Tools.isNotEmpty(info_img)) {
                            Picasso.with(this).load(info_img).into(img_2222);
                        }
                        if (storynumber != null) {
                            storynumber.setText(info_title);
                            storytittle.setText(info_desc);
                        }


                        final ImageView preview_record_img2 = (ImageView) vvv.findViewById(R.id.preview_record_img2);
                        imgList1.add(preview_record_img2);

                        vvv.findViewById(R.id.lin_trylisten).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                stopAnimation2();

                                for (ImageView item : imgList1) {
                                    item.setImageResource(R.drawable.preview_record_img);
                                }

                                releaseMp3Player();

                                if (Tools.isNotEmpty(mPainPointItem.mp3_url)) {
                                    mIsPreviewingStorySong = true;
                                    if (mp3Player == null)
                                        mp3Player = new MediaPlayer();
                                    try {
                                        mp3Player.setDataSource(mPainPointItem.mp3_url);
                                        mp3Player.prepare();
                                        mp3Player.start();
                                        //  startAnimation();
                                        preview_record_img2.setImageResource(R.drawable.preview_anim);
                                        mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                        mAnim.start();

                                        mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                    } catch (IllegalArgumentException e) {
                                        e.printStackTrace();
                                    } catch (SecurityException e) {
                                        e.printStackTrace();
                                    } catch (IllegalStateException e) {
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                } else {
                                    mIsPreviewingStorySong = !mIsPreviewingStorySong;
                                    if (mIsPreviewingStorySong) {
                                        if (mp3Player == null)
                                            mp3Player = new MediaPlayer();
                                        try {
                                            mp3Player.setDataSource(mPainPointItem.mp3_url);
                                            mp3Player.prepare();
                                            mp3Player.start();

                                            preview_record_img2.setImageResource(R.drawable.preview_anim);
                                            mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                            mAnim.start();

                                            mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                        } catch (IllegalArgumentException e) {
                                            e.printStackTrace();
                                        } catch (SecurityException e) {
                                            e.printStackTrace();
                                        } catch (IllegalStateException e) {
                                            e.printStackTrace();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    } else {
                                        stopAnimation2();
                                        releaseMp3Player();

                                        preview_record_img2.setImageResource(R.drawable.preview_record_img);
                                    }
                                }
                            }
                        });
                        // }
                    } else if (mCurrentSelectedEnum == CustomClass) {
                        custom_channel.removeAllViews();
                        imgList2.clear();

                        View vvv = LayoutInflater.from(this).inflate(R.layout.select_class_item,
                                custom_channel, false);

                        custom_channel.addView(vvv);

                        TextView storynumber = (TextView)
                                vvv.findViewById(R.id.storynumber);
                        TextView storytittle = (TextView)
                                vvv.findViewById(R.id.storytittle);
                        TextView tv_storylist = (TextView)
                                vvv.findViewById(R.id.tv_storylist);
                        ImageView img_2222 = (ImageView) vvv.findViewById(R.id.img_2222);


                        if (Tools.isNotEmpty(info_img)) {
                            Picasso.with(this).load(info_img).into(img_2222);
                        }
                        if (storynumber != null) {
                            storynumber.setText(info_title);
                            storytittle.setText(info_desc);
                        }

                        final ImageView preview_record_img2 = (ImageView) vvv.findViewById(R.id.preview_record_img2);
                        imgList2.add(preview_record_img2);


                        vvv.findViewById(R.id.lin_trylisten).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                stopAnimation2();

                                for (ImageView item : imgList2) {
                                    item.setImageResource(R.drawable.preview_record_img);
                                }

                                releaseMp3Player();

                                if (Tools.isNotEmpty(mPainPointItem.mp3_url)) {
                                    mIsPreviewingStorySong = true;
                                    if (mp3Player == null)
                                        mp3Player = new MediaPlayer();
                                    try {
                                        mp3Player.setDataSource(mPainPointItem.mp3_url);
                                        mp3Player.prepare();
                                        mp3Player.start();
                                        //  startAnimation();
                                        preview_record_img2.setImageResource(R.drawable.preview_anim);
                                        mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                        mAnim.start();

                                        mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                    } catch (IllegalArgumentException e) {
                                        e.printStackTrace();
                                    } catch (SecurityException e) {
                                        e.printStackTrace();
                                    } catch (IllegalStateException e) {
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                } else {
                                    mIsPreviewingStorySong = !mIsPreviewingStorySong;
                                    if (mIsPreviewingStorySong) {
                                        if (mp3Player == null)
                                            mp3Player = new MediaPlayer();
                                        try {
                                            mp3Player.setDataSource(mPainPointItem.mp3_url);
                                            mp3Player.prepare();
                                            mp3Player.start();

                                            preview_record_img2.setImageResource(R.drawable.preview_anim);
                                            mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                            mAnim.start();

                                            mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                        } catch (IllegalArgumentException e) {
                                            e.printStackTrace();
                                        } catch (SecurityException e) {
                                            e.printStackTrace();
                                        } catch (IllegalStateException e) {
                                            e.printStackTrace();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    } else {
                                        stopAnimation2();
                                        releaseMp3Player();

                                        preview_record_img2.setImageResource(R.drawable.preview_record_img);
                                    }
                                }
                            }
                        });

                    }
                }

                ViewGroup.LayoutParams pp = mContentLayout.getLayoutParams();
                pp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                mContentLayout.setLayoutParams(pp);

                // rel_abhs.setVisibility(View.VISIBLE);
                if (mPainPointItem.emc == 1) {
                    selectABHS = true;
                    select_cbb.setChecked(true);
                } else {
                    selectABHS = false;
                    select_cbb.setChecked(false);
                }
            } else if (ctype == 2) {
                custom_channel.setVisibility(View.GONE);
                mCurrentSelectedEnum = CustomContent;
                tv_custom11111.setBackgroundResource(R.drawable.round_corner_with_blue_stroke);
                tv_record1111.setBackground(null);
            } else if (ctype == 3) {
                mCurrentSelectedEnum = CustomHabit;
                switchSelectState(mCurrentSelectedEnum);

                if (Tools.isNotEmpty(info_title)) {
                    custom_channel.setVisibility(View.VISIBLE);
                    if (mCurrentSelectedEnum == CommonClass || mCurrentSelectedEnum == CustomHabit) {
                        imgList1.clear();
                        custom_channel.removeAllViews();
                        //  for (int i = 0; i < aiList.size(); i++) {
                        View vvv = LayoutInflater.from(this).inflate(R.layout.select_class_item,
                                custom_channel, false);

                        custom_channel.addView(vvv);

                        TextView storynumber = (TextView)
                                vvv.findViewById(R.id.storynumber);
                        TextView storytittle = (TextView)
                                vvv.findViewById(R.id.storytittle);
                        ImageView img_2222 = (ImageView) vvv.findViewById(R.id.img_2222);

                        if (Tools.isNotEmpty(info_img)) {
                            Picasso.with(this).load(info_img).into(img_2222);
                        }
                        if (storynumber != null) {
                            storynumber.setText(info_title);
                            storytittle.setText(info_desc);
                        }


                        final ImageView preview_record_img2 = (ImageView) vvv.findViewById(R.id.preview_record_img2);
                        imgList1.add(preview_record_img2);

                        vvv.findViewById(R.id.lin_trylisten).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                stopAnimation2();

                                for (ImageView item : imgList1) {
                                    item.setImageResource(R.drawable.preview_record_img);
                                }

                                releaseMp3Player();

                                if (Tools.isNotEmpty(mPainPointItem.mp3_url)) {
                                    mIsPreviewingStorySong = true;
                                    if (mp3Player == null)
                                        mp3Player = new MediaPlayer();
                                    try {
                                        mp3Player.setDataSource(mPainPointItem.mp3_url);
                                        mp3Player.prepare();
                                        mp3Player.start();
                                        //  startAnimation();
                                        preview_record_img2.setImageResource(R.drawable.preview_anim);
                                        mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                        mAnim.start();

                                        mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                    } catch (IllegalArgumentException e) {
                                        e.printStackTrace();
                                    } catch (SecurityException e) {
                                        e.printStackTrace();
                                    } catch (IllegalStateException e) {
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                } else {
                                    mIsPreviewingStorySong = !mIsPreviewingStorySong;
                                    if (mIsPreviewingStorySong) {
                                        if (mp3Player == null)
                                            mp3Player = new MediaPlayer();
                                        try {
                                            mp3Player.setDataSource(mPainPointItem.mp3_url);
                                            mp3Player.prepare();
                                            mp3Player.start();

                                            preview_record_img2.setImageResource(R.drawable.preview_anim);
                                            mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                            mAnim.start();

                                            mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                        } catch (IllegalArgumentException e) {
                                            e.printStackTrace();
                                        } catch (SecurityException e) {
                                            e.printStackTrace();
                                        } catch (IllegalStateException e) {
                                            e.printStackTrace();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    } else {
                                        stopAnimation2();
                                        releaseMp3Player();

                                        preview_record_img2.setImageResource(R.drawable.preview_record_img);
                                    }
                                }
                            }
                        });
                        // }
                    } else if (mCurrentSelectedEnum == CustomClass) {
                        custom_channel.removeAllViews();
                        imgList2.clear();


                        View vvv = LayoutInflater.from(this).inflate(R.layout.select_class_item,
                                custom_channel, false);

                        custom_channel.addView(vvv);

                        TextView storynumber = (TextView)
                                vvv.findViewById(R.id.storynumber);
                        TextView storytittle = (TextView)
                                vvv.findViewById(R.id.storytittle);
                        TextView tv_storylist = (TextView)
                                vvv.findViewById(R.id.tv_storylist);
                        ImageView img_2222 = (ImageView) vvv.findViewById(R.id.img_2222);


                        if (Tools.isNotEmpty(info_img)) {
                            Picasso.with(this).load(info_img).into(img_2222);
                        }
                        if (storynumber != null) {
                            storynumber.setText(info_title);
                            storytittle.setText(info_desc);
                        }

                        final ImageView preview_record_img2 = (ImageView) vvv.findViewById(R.id.preview_record_img2);
                        imgList2.add(preview_record_img2);


                        vvv.findViewById(R.id.lin_trylisten).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                stopAnimation2();

                                for (ImageView item : imgList2) {
                                    item.setImageResource(R.drawable.preview_record_img);
                                }

                                releaseMp3Player();

                                if (Tools.isNotEmpty(mPainPointItem.mp3_url)) {
                                    mIsPreviewingStorySong = true;
                                    if (mp3Player == null)
                                        mp3Player = new MediaPlayer();
                                    try {
                                        mp3Player.setDataSource(mPainPointItem.mp3_url);
                                        mp3Player.prepare();
                                        mp3Player.start();
                                        //  startAnimation();
                                        preview_record_img2.setImageResource(R.drawable.preview_anim);
                                        mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                        mAnim.start();

                                        mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                    } catch (IllegalArgumentException e) {
                                        e.printStackTrace();
                                    } catch (SecurityException e) {
                                        e.printStackTrace();
                                    } catch (IllegalStateException e) {
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                } else {
                                    mIsPreviewingStorySong = !mIsPreviewingStorySong;
                                    if (mIsPreviewingStorySong) {
                                        if (mp3Player == null)
                                            mp3Player = new MediaPlayer();
                                        try {
                                            mp3Player.setDataSource(mPainPointItem.mp3_url);
                                            mp3Player.prepare();
                                            mp3Player.start();

                                            preview_record_img2.setImageResource(R.drawable.preview_anim);
                                            mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                            mAnim.start();

                                            mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                        } catch (IllegalArgumentException e) {
                                            e.printStackTrace();
                                        } catch (SecurityException e) {
                                            e.printStackTrace();
                                        } catch (IllegalStateException e) {
                                            e.printStackTrace();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    } else {
                                        stopAnimation2();
                                        releaseMp3Player();

                                        preview_record_img2.setImageResource(R.drawable.preview_record_img);
                                    }
                                }
                            }
                        });

                    }
                }

                ViewGroup.LayoutParams pp = mContentLayout.getLayoutParams();
                pp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                mContentLayout.setLayoutParams(pp);

                // rel_abhs.setVisibility(View.VISIBLE);
                if (mPainPointItem.emc == 1) {
                    selectABHS = true;
                    select_cbb.setChecked(true);
                } else {
                    selectABHS = false;
                    select_cbb.setChecked(false);
                }
            }
        } else {
            this.mTitleText.setText("添加课程");

            mCurrentSelectedEnum = CustomContent;
            tv_custom11111.setBackgroundResource(R.drawable.round_corner_with_blue_stroke);
            tv_record1111.setBackground(null);
            mTitleEt.setText("通知提醒");
            switchSelectState(mCurrentSelectedEnum);

            //初次进入设置显示系统当前时间
            SimpleDateFormat sf = new SimpleDateFormat("HH:mm");
            String time = sf.format(new Date());
            mSetTimerTv.setText(time);
        }

        loadCustomClassData();
        loadCategoryData();
        loadCustomHabitData();
    }


    //    private int  ctype; //":0,
    private String info_img; //":"",
    private String info_title;// ":"",
    private String info_desc; //":"",

    LeHotDynamicItem ldi;

    private void previewMp3(String mp3_url) {
        releaseMp3Player();

        boolean excepthappen = false;
        if (mp3Player == null)
            mp3Player = new MediaPlayer();
        try {
            mp3Player.setDataSource(mp3_url);
        } catch (IllegalArgumentException e) {
            excepthappen = true;
            e.printStackTrace();
        } catch (SecurityException e) {
            excepthappen = true;
            e.printStackTrace();
        } catch (IllegalStateException e) {
            excepthappen = true;
            e.printStackTrace();
        } catch (IOException e) {
            excepthappen = true;
            e.printStackTrace();
        }

        if (!excepthappen) {
            try {
                mp3Player.prepare();
                mp3Player.start();
                mp3Player.setOnCompletionListener(mediaPlayerListener);
            } catch (IllegalArgumentException e) {
                excepthappen = true;
                e.printStackTrace();
            } catch (SecurityException e) {
                excepthappen = true;
                e.printStackTrace();
            } catch (IllegalStateException e) {
                excepthappen = true;
                e.printStackTrace();
            } catch (IOException e) {
                excepthappen = true;
                e.printStackTrace();
            }
        } else {
            toastShow("暂时无法试听");
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
        processExtraData();
        super.onNewIntent(intent);
    }

    private void processExtraData() {
        Intent intent = getIntent();
        if (intent != null) {
        }
    }

    public static boolean deleteFilesInDir(File path) {

        if (path.exists()) {
            File[] files = path.listFiles();
            if (files == null) {
                return true;
            }
            for (int i = 0; i < files.length; i++) {

                if (files[i].isDirectory()) {

                } else {
                    files[i].delete();
                }
            }
        }
        return true;
    }

    private CountDownTimer mCountDownTimer;

    private void startCountdownTimer() {
        mCountDownTimer = new CountDownTimer(3 * 60 * 1000, 1000) {

            public void onTick(long millisUntilFinished) {
            }

            public void onFinish() {
                showTip("录音时间不超过3分钟");
            }

        }.start();
    }

    private void stopCountdownTimer() {
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
            mCountDownTimer = null;
        }
    }

    private CategoryItem convert2CI(RadioCategoryItem rci) {
        // TODO Auto-generated method stub
        CategoryItem ci = new CategoryItem();
        ci.id = rci.id;
        ci.name = rci.name;
        ci.code = rci.code;
        if (rci.enabled)
            ci.enabled = 1;
        else
            ci.enabled = 0;
        ci.sort = rci.sort;
        ci.created_at = rci.created_at;
        ci.updated_at = rci.updated_at;
        ci.deleted_at = rci.deleted_at;
        ci.icon = rci.icon;
        ci.catalog = rci.catalog;
        ci.home_page = rci.home_page;
        return ci;
    }

    private CategoryItem convert2CI(CustomHabitItem rci) {
        CategoryItem ci = new CategoryItem();
        ci.id = rci.id;
        ci.name = rci.name;
        ci.code = rci.code;
        ci.enabled = rci.enabled;
        ci.sort = rci.sort;
        ci.created_at = rci.created_at;
        ci.updated_at = rci.updated_at;
        ci.deleted_at = rci.deleted_at;
        ci.icon = rci.icon;
        ci.catalog = rci.catalog;
        ci.home_page = rci.home_page;
        return ci;
    }

    private void setRadioCategoryView() {
        mRCGVAdapter = new RadioCategoryGridViewAdapter(this, mRadioCategoryList);
        mRCGVAdapter.setAddHabitMode(true);
        mbottom_gridview.setAdapter(mRCGVAdapter);
        mbottom_gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CategoryItem categoryItem = convert2CI(mRadioCategoryList.get(position));
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                intent.setClass(AddHabitClockActivity.this, ExpandListActivity.class);
                bundle.putSerializable("categoryitem", categoryItem);
                bundle.putBoolean("isEdit", isEdit);
                intent.putExtras(bundle);
                startActivityForResult(intent, 1000);
            }
        });
    }

    private ArrayList<ImageView> imgList1 = new ArrayList<ImageView>();
    private ArrayList<ImageView> imgList2 = new ArrayList<ImageView>();

    private void fillRecomAnimLineLayoutAi() {
        imgList1.clear();
        custom_channel.removeAllViews();
        for (int i = 0; i < aiList.size(); i++) {
            View vvv = LayoutInflater.from(this).inflate(R.layout.select_class_item,
                    custom_channel, false);

            custom_channel.addView(vvv);

            final AlbumItem aiii = aiList.get(i);

            final String album_name = aiii.album_name;
            final String name = aiii.title;
            final String order = aiii.sort + "";
            final String reslen = Tools.secToTime(aiii.length);
            final String subtitle = "";

            final ImageView preview_record_img2 = (ImageView) vvv.findViewById(R.id.preview_record_img2);
            imgList1.add(preview_record_img2);

            vvv.findViewById(R.id.lin_trylisten).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    stopAnimation2();

                    for (ImageView item : imgList1) {
                        item.setImageResource(R.drawable.preview_record_img);
                    }

                    releaseMp3Player();

                    if (Tools.isNotEmpty(aiii.url) /*&& !mAiUrl.equalsIgnoreCase(mp3url_toSubmit)*/) {
                        mIsPreviewingStorySong = true;
                        if (mp3Player == null)
                            mp3Player = new MediaPlayer();
                        try {
                            mp3Player.setDataSource(aiii.url);
                            mp3Player.prepare();
                            mp3Player.start();
                            //  startAnimation();
                            preview_record_img2.setImageResource(R.drawable.preview_anim);
                            mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                            mAnim.start();

                            mp3Player.setOnCompletionListener(mediaPlayerListener2);
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        } catch (SecurityException e) {
                            e.printStackTrace();
                        } catch (IllegalStateException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } else {
                        mIsPreviewingStorySong = !mIsPreviewingStorySong;
                        if (mIsPreviewingStorySong) {
                            if (mp3Player == null)
                                mp3Player = new MediaPlayer();
                            try {
                                mp3Player.setDataSource(aiii.url);
                                mp3Player.prepare();
                                mp3Player.start();

                                preview_record_img2.setImageResource(R.drawable.preview_anim);
                                mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                mAnim.start();

                                mp3Player.setOnCompletionListener(mediaPlayerListener2);
                            } catch (IllegalArgumentException e) {
                                e.printStackTrace();
                            } catch (SecurityException e) {
                                e.printStackTrace();
                            } catch (IllegalStateException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        } else {
                            stopAnimation2();
                            releaseMp3Player();

                            preview_record_img2.setImageResource(R.drawable.preview_record_img);
                        }
                    }
                }
            });


            TextView storynumber =
                    vvv.findViewById(R.id.storynumber);
            TextView storytittle =
                    vvv.findViewById(R.id.storytittle);
            TextView tv_storylist =
                    vvv.findViewById(R.id.tv_storylist);
            ImageView img_2222 = vvv.findViewById(R.id.img_2222);


            if (Tools.isNotEmpty(aiii.album_cover)) {
                Picasso.with(this).load(aiii.album_cover).into(img_2222);
            } else {
                if (Tools.isNotEmpty(info_img)) {
                    Picasso.with(this).load(info_img).into(img_2222);
                }
            }

            if (storynumber != null) {
                storynumber.setText(album_name + " 第" + order + "集" + " ("
                        + reslen + ")");
                if (Tools.isEmpty(subtitle)) {
                    storytittle.setText(name);
                } else {
                    storytittle.setText(subtitle);
                }
            }
        }
    }

    private void fillRecomAnimLineLayoutCAi() {
        custom_channel.removeAllViews();
        imgList2.clear();

        for (int i = 0; i < caiList.size(); i++) {
            View vvv = LayoutInflater.from(this).inflate(R.layout.select_class_item, custom_channel, false);
            custom_channel.addView(vvv);
            final CustomAlbumItem caiii = caiList.get(i);

            final String album_name = caiii.album_name;
            final String name = caiii.title;
            final String order = caiii.sort + "";
            final String reslen = Tools.secToTime(caiii.length);
            final String subtitle = "";

            final ImageView preview_record_img2 = (ImageView) vvv.findViewById(R.id.preview_record_img2);
            imgList2.add(preview_record_img2);

            vvv.findViewById(R.id.lin_trylisten).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    stopAnimation2();

                    for (ImageView item : imgList2) {
                        item.setImageResource(R.drawable.preview_record_img);
                    }

                    releaseMp3Player();

                    if (Tools.isNotEmpty(caiii.url) /*&& !mAiUrl.equalsIgnoreCase(mp3url_toSubmit)*/) {
                        mIsPreviewingStorySong = true;
                        if (mp3Player == null)
                            mp3Player = new MediaPlayer();
                        try {
                            mp3Player.setDataSource(caiii.url);
                            mp3Player.prepare();
                            mp3Player.start();
                            //  startAnimation();
                            preview_record_img2.setImageResource(R.drawable.preview_anim);
                            mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                            mAnim.start();

                            mp3Player.setOnCompletionListener(mediaPlayerListener2);
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        } catch (SecurityException e) {
                            e.printStackTrace();
                        } catch (IllegalStateException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } else {
                        mIsPreviewingStorySong = !mIsPreviewingStorySong;
                        if (mIsPreviewingStorySong) {
                            if (mp3Player == null)
                                mp3Player = new MediaPlayer();
                            try {
                                mp3Player.setDataSource(caiii.url);
                                mp3Player.prepare();
                                mp3Player.start();

                                preview_record_img2.setImageResource(R.drawable.preview_anim);
                                mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                mAnim.start();

                                mp3Player.setOnCompletionListener(mediaPlayerListener2);
                            } catch (IllegalArgumentException e) {
                                e.printStackTrace();
                            } catch (SecurityException e) {
                                e.printStackTrace();
                            } catch (IllegalStateException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        } else {
                            stopAnimation2();
                            releaseMp3Player();

                            preview_record_img2.setImageResource(R.drawable.preview_record_img);
                        }
                    }
                }
            });

            TextView storynumber =
                    vvv.findViewById(R.id.storynumber);
            TextView storytittle =
                    vvv.findViewById(R.id.storytittle);
            TextView tv_storylist =
                    vvv.findViewById(R.id.tv_storylist);
            ImageView img_2222 = vvv.findViewById(R.id.img_2222);


            if (Tools.isNotEmpty(caiii.album_cover)) {
                Picasso.with(this).load(caiii.album_cover).into(img_2222);
            } else {
                if (Tools.isNotEmpty(info_img)) {
                    Picasso.with(this).load(info_img).into(img_2222);
                } else {
                    img_2222.setBackgroundResource(R.drawable.default_icon);
                }
            }

            if (storynumber != null) {
                storynumber.setText(album_name + " 第" + order + "集" + " ("
                        + reslen + ")");
                if (Tools.isEmpty(subtitle)) {
                    storytittle.setText(name);
                } else {
                    storytittle.setText(subtitle);
                }
            }
        }
    }

    ArrayList<AlbumItem> aiList = new ArrayList<AlbumItem>();
    ArrayList<CustomAlbumItem> caiList = new ArrayList<CustomAlbumItem>();

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent returnedIntent) {
        super.onActivityResult(requestCode, resultCode, returnedIntent);
        switch (requestCode) {
            case 1002:
            case 1000:
                if (resultCode == RESULT_OK) { // 大众课程&习惯养成

                    aiList = (ArrayList<AlbumItem>) returnedIntent.getExtras()
                            .getSerializable("selectdata");

                    mTitleEt.setText(aiList.get(0).album_name);

                    fillRecomAnimLineLayoutAi();
                    custom_channel.setVisibility(View.VISIBLE);
                    if (!isEdit) {
                        rel_abhs.setVisibility(View.VISIBLE);
                        rel_yxkc.setVisibility(View.VISIBLE);
                        view_lllll.setVisibility(View.VISIBLE);
                        if (selectABHS)
                            lin_abhs.setVisibility(View.VISIBLE);

                        tv_cv1.setText("1. " + mCurDay + "   " + mSetTimerTv.getText().toString());
                        tv_cv2.setText("2. " + mCurDay2 + "   " + mSetTimerTv.getText().toString());
                        tv_cv3.setText("3. " + mCurDay3 + "   " + mSetTimerTv.getText().toString());
                        tv_cv4.setText("4. " + mCurDay4 + "   " + mSetTimerTv.getText().toString());
                        tv_cv5.setText("5. " + mCurDay5 + "   " + mSetTimerTv.getText().toString());
                        tv_cv6.setText("6. " + mCurDay6 + "   " + mSetTimerTv.getText().toString());
                        tv_cv7.setText("7. " + mCurDay7 + "   " + mSetTimerTv.getText().toString());
                    }
                }
                break;
            case 1001:
                if (resultCode == RESULT_OK) { // 课程课件
                    caiList = (ArrayList<CustomAlbumItem>) returnedIntent.getExtras()
                            .getSerializable("selectdata");

                    fillRecomAnimLineLayoutCAi();

                    mTitleEt.setText(caiList.get(0).album_name);

                    custom_channel.setVisibility(View.VISIBLE);
                    if (!isEdit) {
                        rel_abhs.setVisibility(View.VISIBLE);
                        rel_yxkc.setVisibility(View.VISIBLE);
                        view_lllll.setVisibility(View.VISIBLE);
                        if (selectABHS)
                            lin_abhs.setVisibility(View.VISIBLE);

                        tv_cv1.setText("1. " + mCurDay + "   " + mSetTimerTv.getText().toString());
                        tv_cv2.setText("2. " + mCurDay2 + "   " + mSetTimerTv.getText().toString());
                        tv_cv3.setText("3. " + mCurDay3 + "   " + mSetTimerTv.getText().toString());
                        tv_cv4.setText("4. " + mCurDay4 + "   " + mSetTimerTv.getText().toString());
                        tv_cv5.setText("5. " + mCurDay5 + "   " + mSetTimerTv.getText().toString());
                        tv_cv6.setText("6. " + mCurDay6 + "   " + mSetTimerTv.getText().toString());
                        tv_cv7.setText("7. " + mCurDay7 + "   " + mSetTimerTv.getText().toString());
                    }
                }
                break;
        }
    }

    private int ctype = 2; //  0:课程课件 1:大众课程 2: 通知提醒 3：习惯养成
    private int originalctype = 2;

    private void switchSelectState(SelectedEnum se) {
        switch (se) {
            case CustomContent: // 自定义
                ctype = 2;
                mSelectedImg.setImageResource(R.drawable.notify_selected);
                mSaveUploadBtn.setVisibility(View.VISIBLE);
                mRecordStateBtn.setVisibility(View.GONE);
                mRetryTv.setVisibility(View.GONE);
                mTtsContentEt.setVisibility(View.VISIBLE);
                mRecordingTipTv.setVisibility(View.GONE);
                mRecordVisualizerView.setVisibility(View.GONE);
                img_emptystory.setVisibility(View.GONE);
                mbottom_gridview.setVisibility(View.GONE);
                customclass_gridview.setVisibility(View.GONE);
                custom_channel.setVisibility(View.GONE);
                mPreviewImg.setVisibility(View.VISIBLE);
                mPreviewListenTv.setVisibility(View.VISIBLE);
                rel_notify.setVisibility(View.VISIBLE);
                rel_abhs.setVisibility(View.GONE);
                rel_yxkc.setVisibility(View.GONE);
                view_lllll.setVisibility(View.GONE);
                lin_abhs.setVisibility(View.GONE);
                custom_channel.setVisibility(View.GONE);
                customhabit_gridview.setVisibility(View.GONE);
                restoreContentLayoutHeight();
                break;
            case Recording: // 录音
                ctype = 2;
                mSelectedImg.setImageResource(R.drawable.notify_selected);
                mSaveUploadBtn.setVisibility(View.GONE);
                mRecordStateBtn.setVisibility(View.VISIBLE);
                mRetryTv.setVisibility(View.VISIBLE);
                mRecordVisualizerView.setVisibility(View.VISIBLE);
                mTtsContentEt.setVisibility(View.GONE);
                mRecordingTipTv.setVisibility(View.VISIBLE);
                img_emptystory.setVisibility(View.GONE);
                mbottom_gridview.setVisibility(View.GONE);
                customclass_gridview.setVisibility(View.GONE);
                custom_channel.setVisibility(View.GONE);
                mPreviewImg.setVisibility(View.VISIBLE);
                mPreviewListenTv.setVisibility(View.VISIBLE);
                rel_notify.setVisibility(View.VISIBLE);
                rel_abhs.setVisibility(View.GONE);
                rel_yxkc.setVisibility(View.GONE);
                lin_abhs.setVisibility(View.GONE);
                view_lllll.setVisibility(View.GONE);
                customhabit_gridview.setVisibility(View.GONE);
                restoreContentLayoutHeight();
                break;
            case CustomClass: // 课程课件
                ctype = 0;
                mSelectedImg.setImageResource(R.drawable.custom_class_selected);
                mRecordStateBtn.setVisibility(View.GONE);
                mRetryTv.setVisibility(View.GONE);
                mTtsContentEt.setVisibility(View.GONE);
                mRecordingTipTv.setVisibility(View.GONE);
                mRecordVisualizerView.setVisibility(View.GONE);
                mSaveUploadBtn.setVisibility(View.GONE);
                mPreviewImg.setVisibility(View.GONE);
                mPreviewListenTv.setVisibility(View.GONE);
                img_emptystory.setVisibility(View.GONE);
                mbottom_gridview.setVisibility(View.GONE);
                customclass_gridview.setVisibility(View.VISIBLE);
                customhabit_gridview.setVisibility(View.GONE);
//                rel_abhs.setVisibility(View.VISIBLE);
                rel_notify.setVisibility(View.GONE);
                restoreContentLayoutHeight();
                break;
            case CommonClass: // 大众课程
                ctype = 1;
                mSelectedImg.setImageResource(R.drawable.common_class_selected);
                mRecordStateBtn.setVisibility(View.GONE);
                mRetryTv.setVisibility(View.GONE);
                mTtsContentEt.setVisibility(View.GONE);
                mRecordingTipTv.setVisibility(View.GONE);
                mRecordVisualizerView.setVisibility(View.GONE);
                mSaveUploadBtn.setVisibility(View.GONE);
                mPreviewImg.setVisibility(View.GONE);
                mPreviewListenTv.setVisibility(View.GONE);
                img_emptystory.setVisibility(View.GONE);
//                rel_abhs.setVisibility(View.VISIBLE);
                customclass_gridview.setVisibility(View.GONE);
                mbottom_gridview.setVisibility(View.VISIBLE);
                rel_notify.setVisibility(View.GONE);

                customhabit_gridview.setVisibility(View.GONE);

                restoreContentLayoutHeight();
                break;

            case CustomHabit: // 习惯定制
                ctype = 3;
                mSelectedImg.setImageResource(R.drawable.common_habit_selected);
                mRecordStateBtn.setVisibility(View.GONE);
                mRetryTv.setVisibility(View.GONE);
                mTtsContentEt.setVisibility(View.GONE);
                mRecordingTipTv.setVisibility(View.GONE);
                mRecordVisualizerView.setVisibility(View.GONE);
                mSaveUploadBtn.setVisibility(View.GONE);
                mPreviewImg.setVisibility(View.GONE);
                mPreviewListenTv.setVisibility(View.GONE);
                img_emptystory.setVisibility(View.GONE);
                mbottom_gridview.setVisibility(View.GONE);
                customclass_gridview.setVisibility(View.GONE);
                customhabit_gridview.setVisibility(View.VISIBLE);
//                rel_abhs.setVisibility(View.VISIBLE);
                rel_notify.setVisibility(View.GONE);

                restoreContentLayoutHeight();
                break;
            default:
                break;
        }
    }

    private String mAiUrl;
    private boolean mIsPreviewingStorySong;

    private void resetValue() {
        releaseMp3Player();
        stopAnimation();
        stopAnimation2();
        custom_channel.setVisibility(View.GONE);
        rel_abhs.setVisibility(View.GONE);
        rel_yxkc.setVisibility(View.GONE);
        lin_abhs.setVisibility(View.GONE);
        view_lllll.setVisibility(View.GONE);
        mp3url_toSubmit = "";
    }

    private String beginDate, endDate;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rel_yxkc:
                final CustomEditDialog.Builder customBuilder = new CustomEditDialog.Builder(
                        AddHabitClockActivity.this);
                customBuilder.setTitle("设置预习课程时段")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        beginDate = customBuilder.getEditText();
                        endDate = customBuilder.getEditText2();
                        if (Tools.isNotEmpty(beginDate) && Tools.isNotEmpty(endDate)) {
                            // bindDevice(dialog, false, mDeviceID.toLowerCase(),nick_name);
                            dialog.dismiss();
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            try {
                                mCurDay = sdf.format(Tools.getDateAfter(sdf.parse(endDate), 1));
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }

                            try {
                                Date date2 = Tools.getDateAfter(sdf.parse(mCurDay), 1);
                                Date date3 = Tools.getDateAfter(sdf.parse(mCurDay), 3);
                                Date date4 = Tools.getDateAfter(sdf.parse(mCurDay), 6);
                                Date date5 = Tools.getDateAfter(sdf.parse(mCurDay), 13);
                                Date date6 = Tools.getDateAfter(sdf.parse(mCurDay), 27);
                                Date date7 = Tools.getDateAfter(sdf.parse(mCurDay), 59);

                                mCurDay2 = sdf.format(date2);
                                mCurDay3 = sdf.format(date3);
                                mCurDay4 = sdf.format(date4);
                                mCurDay5 = sdf.format(date5);
                                mCurDay6 = sdf.format(date6);
                                mCurDay7 = sdf.format(date7);

                            } catch (ParseException e) {
                                e.printStackTrace();
                            }


                        } else {
                            toastShow("日期不能为空!");
                        }
                    }
                }).setEditTextBoth("", "");

                if (!isFinishing()) {
                    customBuilder.create().show();
                }
                break;
            case R.id.lin_trylisten:
                break;
            case R.id.custom_class_btn: // 课程课件
                if (!isEdit) {
                    resetValue();
                    mTitleEt.setText("");
                } else {
                    if (originalctype == 0) {
                        if (Tools.isNotEmpty(info_title)) {
                            custom_channel.setVisibility(View.VISIBLE);


                            if (!isEdit) {
                                rel_abhs.setVisibility(View.VISIBLE);
                                rel_yxkc.setVisibility(View.VISIBLE);
                                view_lllll.setVisibility(View.VISIBLE);
                                if (selectABHS)
                                    lin_abhs.setVisibility(View.VISIBLE);

                                tv_cv1.setText("1. " + mCurDay + "   " + mSetTimerTv.getText().toString());
                                tv_cv2.setText("2. " + mCurDay2 + "   " + mSetTimerTv.getText().toString());
                                tv_cv3.setText("3. " + mCurDay3 + "   " + mSetTimerTv.getText().toString());
                                tv_cv4.setText("4. " + mCurDay4 + "   " + mSetTimerTv.getText().toString());
                                tv_cv5.setText("5. " + mCurDay5 + "   " + mSetTimerTv.getText().toString());
                                tv_cv6.setText("6. " + mCurDay6 + "   " + mSetTimerTv.getText().toString());
                                tv_cv7.setText("7. " + mCurDay7 + "   " + mSetTimerTv.getText().toString());
                            }
                            if (mPainPointItem.emc == 1) {
                                selectABHS = true;
                                select_cbb.setChecked(true);
                            } else {
                                selectABHS = false;
                                select_cbb.setChecked(false);
                            }

                            if (mCurrentSelectedEnum == CommonClass || mCurrentSelectedEnum == CustomHabit) {
                                imgList1.clear();
                                custom_channel.removeAllViews();
                                //  for (int i = 0; i < aiList.size(); i++) {
                                View vvv = LayoutInflater.from(this).inflate(R.layout.select_class_item,
                                        custom_channel, false);

                                custom_channel.addView(vvv);

                                TextView storynumber = (TextView)
                                        vvv.findViewById(R.id.storynumber);
                                TextView storytittle = (TextView)
                                        vvv.findViewById(R.id.storytittle);
                                ImageView img_2222 = (ImageView) vvv.findViewById(R.id.img_2222);

                                if (Tools.isNotEmpty(info_img)) {
                                    Picasso.with(this).load(info_img).into(img_2222);
                                }
                                if (storynumber != null) {
                                    storynumber.setText(info_title);
                                    storytittle.setText(info_desc);
                                }


                                final ImageView preview_record_img2 = (ImageView) vvv.findViewById(R.id.preview_record_img2);
                                imgList1.add(preview_record_img2);

                                vvv.findViewById(R.id.lin_trylisten).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        stopAnimation2();

                                        for (ImageView item : imgList1) {
                                            item.setImageResource(R.drawable.preview_record_img);
                                        }

                                        releaseMp3Player();

                                        if (Tools.isNotEmpty(mPainPointItem.mp3_url)) {
                                            mIsPreviewingStorySong = true;
                                            if (mp3Player == null)
                                                mp3Player = new MediaPlayer();
                                            try {
                                                mp3Player.setDataSource(mPainPointItem.mp3_url);
                                                mp3Player.prepare();
                                                mp3Player.start();
                                                //  startAnimation();
                                                preview_record_img2.setImageResource(R.drawable.preview_anim);
                                                mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                                mAnim.start();

                                                mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                            } catch (IllegalArgumentException e) {
                                                e.printStackTrace();
                                            } catch (SecurityException e) {
                                                e.printStackTrace();
                                            } catch (IllegalStateException e) {
                                                e.printStackTrace();
                                            } catch (IOException e) {
                                                e.printStackTrace();
                                            }
                                        } else {
                                            mIsPreviewingStorySong = !mIsPreviewingStorySong;
                                            if (mIsPreviewingStorySong) {
                                                if (mp3Player == null)
                                                    mp3Player = new MediaPlayer();
                                                try {
                                                    mp3Player.setDataSource(mPainPointItem.mp3_url);
                                                    mp3Player.prepare();
                                                    mp3Player.start();

                                                    preview_record_img2.setImageResource(R.drawable.preview_anim);
                                                    mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                                    mAnim.start();

                                                    mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                                } catch (IllegalArgumentException e) {
                                                    e.printStackTrace();
                                                } catch (SecurityException e) {
                                                    e.printStackTrace();
                                                } catch (IllegalStateException e) {
                                                    e.printStackTrace();
                                                } catch (IOException e) {
                                                    e.printStackTrace();
                                                }
                                            } else {
                                                stopAnimation2();
                                                releaseMp3Player();

                                                preview_record_img2.setImageResource(R.drawable.preview_record_img);
                                            }
                                        }
                                    }
                                });
                                // }
                            } else if (mCurrentSelectedEnum == CustomClass) {
                                custom_channel.removeAllViews();
                                imgList2.clear();

                                for (int i = 0; i < caiList.size(); i++) {
                                    View vvv = LayoutInflater.from(this).inflate(R.layout.select_class_item,
                                            custom_channel, false);

                                    custom_channel.addView(vvv);

                                    TextView storynumber = (TextView)
                                            vvv.findViewById(R.id.storynumber);
                                    TextView storytittle = (TextView)
                                            vvv.findViewById(R.id.storytittle);
                                    TextView tv_storylist = (TextView)
                                            vvv.findViewById(R.id.tv_storylist);
                                    ImageView img_2222 = (ImageView) vvv.findViewById(R.id.img_2222);


                                    if (Tools.isNotEmpty(info_img)) {
                                        Picasso.with(this).load(info_img).into(img_2222);
                                    }
                                    if (storynumber != null) {
                                        storynumber.setText(info_title);
                                        storytittle.setText(info_desc);
                                    }

                                    final ImageView preview_record_img2 = (ImageView) vvv.findViewById(R.id.preview_record_img2);
                                    imgList2.add(preview_record_img2);


                                    vvv.findViewById(R.id.lin_trylisten).setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            stopAnimation2();

                                            for (ImageView item : imgList2) {
                                                item.setImageResource(R.drawable.preview_record_img);
                                            }

                                            releaseMp3Player();

                                            if (Tools.isNotEmpty(mPainPointItem.mp3_url)) {
                                                mIsPreviewingStorySong = true;
                                                if (mp3Player == null)
                                                    mp3Player = new MediaPlayer();
                                                try {
                                                    mp3Player.setDataSource(mPainPointItem.mp3_url);
                                                    mp3Player.prepare();
                                                    mp3Player.start();
                                                    //  startAnimation();
                                                    preview_record_img2.setImageResource(R.drawable.preview_anim);
                                                    mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                                    mAnim.start();

                                                    mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                                } catch (IllegalArgumentException e) {
                                                    e.printStackTrace();
                                                } catch (SecurityException e) {
                                                    e.printStackTrace();
                                                } catch (IllegalStateException e) {
                                                    e.printStackTrace();
                                                } catch (IOException e) {
                                                    e.printStackTrace();
                                                }
                                            } else {
                                                mIsPreviewingStorySong = !mIsPreviewingStorySong;
                                                if (mIsPreviewingStorySong) {
                                                    if (mp3Player == null)
                                                        mp3Player = new MediaPlayer();
                                                    try {
                                                        mp3Player.setDataSource(mPainPointItem.mp3_url);
                                                        mp3Player.prepare();
                                                        mp3Player.start();

                                                        preview_record_img2.setImageResource(R.drawable.preview_anim);
                                                        mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                                        mAnim.start();

                                                        mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                                    } catch (IllegalArgumentException e) {
                                                        e.printStackTrace();
                                                    } catch (SecurityException e) {
                                                        e.printStackTrace();
                                                    } catch (IllegalStateException e) {
                                                        e.printStackTrace();
                                                    } catch (IOException e) {
                                                        e.printStackTrace();
                                                    }
                                                } else {
                                                    stopAnimation2();
                                                    releaseMp3Player();

                                                    preview_record_img2.setImageResource(R.drawable.preview_record_img);
                                                }
                                            }
                                        }
                                    });


                                }
                            }

                        }
                        mTitleEt.setText(mPainPointItem.name);
                    } else {
                        resetValue();
                        mTitleEt.setText("");
                    }

                    releaseMp3Player();
                    stopAnimation();
                    stopAnimation2();
                }
                mCurrentSelectedEnum = CustomClass;
                switchSelectState(mCurrentSelectedEnum);


                selectABHS = false;
                ViewGroup.LayoutParams pp = mContentLayout.getLayoutParams();
                pp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                mContentLayout.setLayoutParams(pp);
                break;
            case R.id.common_class_btn: // 大众课程
                if (!isEdit) {
                    resetValue();
                    mTitleEt.setText("");
                } else {
                    if (originalctype == 1) {
                        mCurrentSelectedEnum = CommonClass;
                        switchSelectState(mCurrentSelectedEnum);

                        if (Tools.isNotEmpty(info_title)) {
                            custom_channel.setVisibility(View.VISIBLE);


                            if (!isEdit) {
                                rel_abhs.setVisibility(View.VISIBLE);
                                rel_yxkc.setVisibility(View.VISIBLE);
                                view_lllll.setVisibility(View.VISIBLE);
                                if (selectABHS)
                                    lin_abhs.setVisibility(View.VISIBLE);

                                tv_cv1.setText("1. " + mCurDay + "   " + mSetTimerTv.getText().toString());
                                tv_cv2.setText("2. " + mCurDay2 + "   " + mSetTimerTv.getText().toString());
                                tv_cv3.setText("3. " + mCurDay3 + "   " + mSetTimerTv.getText().toString());
                                tv_cv4.setText("4. " + mCurDay4 + "   " + mSetTimerTv.getText().toString());
                                tv_cv5.setText("5. " + mCurDay5 + "   " + mSetTimerTv.getText().toString());
                                tv_cv6.setText("6. " + mCurDay6 + "   " + mSetTimerTv.getText().toString());
                                tv_cv7.setText("7. " + mCurDay7 + "   " + mSetTimerTv.getText().toString());
                            }

                            if (mPainPointItem.emc == 1) {
                                selectABHS = true;
                                select_cbb.setChecked(true);
                            } else {
                                selectABHS = false;
                                select_cbb.setChecked(false);
                            }

                            if (mCurrentSelectedEnum == CommonClass || mCurrentSelectedEnum == CustomHabit) {
                                imgList1.clear();
                                custom_channel.removeAllViews();
                                //  for (int i = 0; i < aiList.size(); i++) {
                                View vvv = LayoutInflater.from(this).inflate(R.layout.select_class_item,
                                        custom_channel, false);

                                custom_channel.addView(vvv);

                                TextView storynumber = (TextView)
                                        vvv.findViewById(R.id.storynumber);
                                TextView storytittle = (TextView)
                                        vvv.findViewById(R.id.storytittle);
                                ImageView img_2222 = (ImageView) vvv.findViewById(R.id.img_2222);

                                if (Tools.isNotEmpty(info_img)) {
                                    Picasso.with(this).load(info_img).into(img_2222);
                                }
                                if (storynumber != null) {
                                    storynumber.setText(info_title);
                                    storytittle.setText(info_desc);
                                }


                                final ImageView preview_record_img2 = (ImageView) vvv.findViewById(R.id.preview_record_img2);
                                imgList1.add(preview_record_img2);

                                vvv.findViewById(R.id.lin_trylisten).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        stopAnimation2();

                                        for (ImageView item : imgList1) {
                                            item.setImageResource(R.drawable.preview_record_img);
                                        }

                                        releaseMp3Player();

                                        if (Tools.isNotEmpty(mPainPointItem.mp3_url)) {
                                            mIsPreviewingStorySong = true;
                                            if (mp3Player == null)
                                                mp3Player = new MediaPlayer();
                                            try {
                                                mp3Player.setDataSource(mPainPointItem.mp3_url);
                                                mp3Player.prepare();
                                                mp3Player.start();
                                                //  startAnimation();
                                                preview_record_img2.setImageResource(R.drawable.preview_anim);
                                                mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                                mAnim.start();

                                                mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                            } catch (IllegalArgumentException e) {
                                                e.printStackTrace();
                                            } catch (SecurityException e) {
                                                e.printStackTrace();
                                            } catch (IllegalStateException e) {
                                                e.printStackTrace();
                                            } catch (IOException e) {
                                                e.printStackTrace();
                                            }
                                        } else {
                                            mIsPreviewingStorySong = !mIsPreviewingStorySong;
                                            if (mIsPreviewingStorySong) {
                                                if (mp3Player == null)
                                                    mp3Player = new MediaPlayer();
                                                try {
                                                    mp3Player.setDataSource(mPainPointItem.mp3_url);
                                                    mp3Player.prepare();
                                                    mp3Player.start();

                                                    preview_record_img2.setImageResource(R.drawable.preview_anim);
                                                    mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                                    mAnim.start();

                                                    mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                                } catch (IllegalArgumentException e) {
                                                    e.printStackTrace();
                                                } catch (SecurityException e) {
                                                    e.printStackTrace();
                                                } catch (IllegalStateException e) {
                                                    e.printStackTrace();
                                                } catch (IOException e) {
                                                    e.printStackTrace();
                                                }
                                            } else {
                                                stopAnimation2();
                                                releaseMp3Player();

                                                preview_record_img2.setImageResource(R.drawable.preview_record_img);
                                            }
                                        }
                                    }
                                });
                                // }
                            } else if (mCurrentSelectedEnum == CustomClass) {
                                custom_channel.removeAllViews();
                                imgList2.clear();

                                for (int i = 0; i < caiList.size(); i++) {
                                    View vvv = LayoutInflater.from(this).inflate(R.layout.select_class_item,
                                            custom_channel, false);

                                    custom_channel.addView(vvv);

                                    TextView storynumber = (TextView)
                                            vvv.findViewById(R.id.storynumber);
                                    TextView storytittle = (TextView)
                                            vvv.findViewById(R.id.storytittle);
                                    TextView tv_storylist = (TextView)
                                            vvv.findViewById(R.id.tv_storylist);
                                    ImageView img_2222 = (ImageView) vvv.findViewById(R.id.img_2222);


                                    if (Tools.isNotEmpty(info_img)) {
                                        Picasso.with(this).load(info_img).into(img_2222);
                                    }
                                    if (storynumber != null) {
                                        storynumber.setText(info_title);
                                        storytittle.setText(info_desc);
                                    }

                                    final ImageView preview_record_img2 = (ImageView) vvv.findViewById(R.id.preview_record_img2);
                                    imgList2.add(preview_record_img2);


                                    vvv.findViewById(R.id.lin_trylisten).setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            stopAnimation2();

                                            for (ImageView item : imgList2) {
                                                item.setImageResource(R.drawable.preview_record_img);
                                            }

                                            releaseMp3Player();

                                            if (Tools.isNotEmpty(mPainPointItem.mp3_url)) {
                                                mIsPreviewingStorySong = true;
                                                if (mp3Player == null)
                                                    mp3Player = new MediaPlayer();
                                                try {
                                                    mp3Player.setDataSource(mPainPointItem.mp3_url);
                                                    mp3Player.prepare();
                                                    mp3Player.start();
                                                    //  startAnimation();
                                                    preview_record_img2.setImageResource(R.drawable.preview_anim);
                                                    mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                                    mAnim.start();

                                                    mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                                } catch (IllegalArgumentException e) {
                                                    e.printStackTrace();
                                                } catch (SecurityException e) {
                                                    e.printStackTrace();
                                                } catch (IllegalStateException e) {
                                                    e.printStackTrace();
                                                } catch (IOException e) {
                                                    e.printStackTrace();
                                                }
                                            } else {
                                                mIsPreviewingStorySong = !mIsPreviewingStorySong;
                                                if (mIsPreviewingStorySong) {
                                                    if (mp3Player == null)
                                                        mp3Player = new MediaPlayer();
                                                    try {
                                                        mp3Player.setDataSource(mPainPointItem.mp3_url);
                                                        mp3Player.prepare();
                                                        mp3Player.start();

                                                        preview_record_img2.setImageResource(R.drawable.preview_anim);
                                                        mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                                        mAnim.start();

                                                        mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                                    } catch (IllegalArgumentException e) {
                                                        e.printStackTrace();
                                                    } catch (SecurityException e) {
                                                        e.printStackTrace();
                                                    } catch (IllegalStateException e) {
                                                        e.printStackTrace();
                                                    } catch (IOException e) {
                                                        e.printStackTrace();
                                                    }
                                                } else {
                                                    stopAnimation2();
                                                    releaseMp3Player();

                                                    preview_record_img2.setImageResource(R.drawable.preview_record_img);
                                                }
                                            }
                                        }
                                    });


                                }
                            }
                        }
                        mTitleEt.setText(mPainPointItem.name);
                    } else {
                        resetValue();
                        mTitleEt.setText("");
                    }

                    releaseMp3Player();
                    stopAnimation();
                    stopAnimation2();
                }
                mCurrentSelectedEnum = CommonClass;
                switchSelectState(mCurrentSelectedEnum);

                selectABHS = false;
                ViewGroup.LayoutParams pp2 = mContentLayout.getLayoutParams();
                pp2.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                mContentLayout.setLayoutParams(pp2);
                break;
            case R.id.customhabit_btn:  // 习惯养成
                if (!isEdit) {
                    resetValue();
                    mTitleEt.setText("");
                } else {
                    if (originalctype == 3) {
                        mCurrentSelectedEnum = CustomHabit;
                        switchSelectState(mCurrentSelectedEnum);

                        if (Tools.isNotEmpty(info_title)) {
                            custom_channel.setVisibility(View.VISIBLE);

                            if (!isEdit) {
                                rel_abhs.setVisibility(View.VISIBLE);
                                rel_yxkc.setVisibility(View.VISIBLE);
                                view_lllll.setVisibility(View.VISIBLE);
                                if (selectABHS)
                                    lin_abhs.setVisibility(View.VISIBLE);

                                tv_cv1.setText("1. " + mCurDay + "   " + mSetTimerTv.getText().toString());
                                tv_cv2.setText("2. " + mCurDay2 + "   " + mSetTimerTv.getText().toString());
                                tv_cv3.setText("3. " + mCurDay3 + "   " + mSetTimerTv.getText().toString());
                                tv_cv4.setText("4. " + mCurDay4 + "   " + mSetTimerTv.getText().toString());
                                tv_cv5.setText("5. " + mCurDay5 + "   " + mSetTimerTv.getText().toString());
                                tv_cv6.setText("6. " + mCurDay6 + "   " + mSetTimerTv.getText().toString());
                                tv_cv7.setText("7. " + mCurDay7 + "   " + mSetTimerTv.getText().toString());
                            }

                            if (mPainPointItem.emc == 1) {
                                selectABHS = true;
                                select_cbb.setChecked(true);
                            } else {
                                selectABHS = false;
                                select_cbb.setChecked(false);
                            }

                            if (mCurrentSelectedEnum == CommonClass || mCurrentSelectedEnum == CustomHabit) {
                                imgList1.clear();
                                custom_channel.removeAllViews();
                                //  for (int i = 0; i < aiList.size(); i++) {
                                View vvv = LayoutInflater.from(this).inflate(R.layout.select_class_item,
                                        custom_channel, false);

                                custom_channel.addView(vvv);

                                TextView storynumber = (TextView)
                                        vvv.findViewById(R.id.storynumber);
                                TextView storytittle = (TextView)
                                        vvv.findViewById(R.id.storytittle);
                                ImageView img_2222 = (ImageView) vvv.findViewById(R.id.img_2222);

                                if (Tools.isNotEmpty(info_img)) {
                                    Picasso.with(this).load(info_img).into(img_2222);
                                }
                                if (storynumber != null) {
                                    storynumber.setText(info_title);
                                    storytittle.setText(info_desc);
                                }


                                final ImageView preview_record_img2 = (ImageView) vvv.findViewById(R.id.preview_record_img2);
                                imgList1.add(preview_record_img2);

                                vvv.findViewById(R.id.lin_trylisten).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        stopAnimation2();

                                        for (ImageView item : imgList1) {
                                            item.setImageResource(R.drawable.preview_record_img);
                                        }

                                        releaseMp3Player();

                                        if (Tools.isNotEmpty(mPainPointItem.mp3_url)) {
                                            mIsPreviewingStorySong = true;
                                            if (mp3Player == null)
                                                mp3Player = new MediaPlayer();
                                            try {
                                                mp3Player.setDataSource(mPainPointItem.mp3_url);
                                                mp3Player.prepare();
                                                mp3Player.start();
                                                //  startAnimation();
                                                preview_record_img2.setImageResource(R.drawable.preview_anim);
                                                mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                                mAnim.start();

                                                mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                            } catch (IllegalArgumentException e) {
                                                e.printStackTrace();
                                            } catch (SecurityException e) {
                                                e.printStackTrace();
                                            } catch (IllegalStateException e) {
                                                e.printStackTrace();
                                            } catch (IOException e) {
                                                e.printStackTrace();
                                            }
                                        } else {
                                            mIsPreviewingStorySong = !mIsPreviewingStorySong;
                                            if (mIsPreviewingStorySong) {
                                                if (mp3Player == null)
                                                    mp3Player = new MediaPlayer();
                                                try {
                                                    mp3Player.setDataSource(mPainPointItem.mp3_url);
                                                    mp3Player.prepare();
                                                    mp3Player.start();

                                                    preview_record_img2.setImageResource(R.drawable.preview_anim);
                                                    mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                                    mAnim.start();

                                                    mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                                } catch (IllegalArgumentException e) {
                                                    e.printStackTrace();
                                                } catch (SecurityException e) {
                                                    e.printStackTrace();
                                                } catch (IllegalStateException e) {
                                                    e.printStackTrace();
                                                } catch (IOException e) {
                                                    e.printStackTrace();
                                                }
                                            } else {
                                                stopAnimation2();
                                                releaseMp3Player();

                                                preview_record_img2.setImageResource(R.drawable.preview_record_img);
                                            }
                                        }
                                    }
                                });
                                // }
                            } else if (mCurrentSelectedEnum == CustomClass) {
                                custom_channel.removeAllViews();
                                imgList2.clear();

                                for (int i = 0; i < caiList.size(); i++) {
                                    View vvv = LayoutInflater.from(this).inflate(R.layout.select_class_item,
                                            custom_channel, false);

                                    custom_channel.addView(vvv);

                                    TextView storynumber = (TextView)
                                            vvv.findViewById(R.id.storynumber);
                                    TextView storytittle = (TextView)
                                            vvv.findViewById(R.id.storytittle);
                                    TextView tv_storylist = (TextView)
                                            vvv.findViewById(R.id.tv_storylist);
                                    ImageView img_2222 = (ImageView) vvv.findViewById(R.id.img_2222);


                                    if (Tools.isNotEmpty(info_img)) {
                                        Picasso.with(this).load(info_img).into(img_2222);
                                    }
                                    if (storynumber != null) {
                                        storynumber.setText(info_title);
                                        storytittle.setText(info_desc);
                                    }

                                    final ImageView preview_record_img2 = (ImageView) vvv.findViewById(R.id.preview_record_img2);
                                    imgList2.add(preview_record_img2);


                                    vvv.findViewById(R.id.lin_trylisten).setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            stopAnimation2();

                                            for (ImageView item : imgList2) {
                                                item.setImageResource(R.drawable.preview_record_img);
                                            }

                                            releaseMp3Player();

                                            if (Tools.isNotEmpty(mPainPointItem.mp3_url)) {
                                                mIsPreviewingStorySong = true;
                                                if (mp3Player == null)
                                                    mp3Player = new MediaPlayer();
                                                try {
                                                    mp3Player.setDataSource(mPainPointItem.mp3_url);
                                                    mp3Player.prepare();
                                                    mp3Player.start();
                                                    //  startAnimation();
                                                    preview_record_img2.setImageResource(R.drawable.preview_anim);
                                                    mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                                    mAnim.start();

                                                    mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                                } catch (IllegalArgumentException e) {
                                                    e.printStackTrace();
                                                } catch (SecurityException e) {
                                                    e.printStackTrace();
                                                } catch (IllegalStateException e) {
                                                    e.printStackTrace();
                                                } catch (IOException e) {
                                                    e.printStackTrace();
                                                }
                                            } else {
                                                mIsPreviewingStorySong = !mIsPreviewingStorySong;
                                                if (mIsPreviewingStorySong) {
                                                    if (mp3Player == null)
                                                        mp3Player = new MediaPlayer();
                                                    try {
                                                        mp3Player.setDataSource(mPainPointItem.mp3_url);
                                                        mp3Player.prepare();
                                                        mp3Player.start();

                                                        preview_record_img2.setImageResource(R.drawable.preview_anim);
                                                        mAnim = (AnimationDrawable) preview_record_img2.getDrawable();
                                                        mAnim.start();

                                                        mp3Player.setOnCompletionListener(mediaPlayerListener2);
                                                    } catch (IllegalArgumentException e) {
                                                        e.printStackTrace();
                                                    } catch (SecurityException e) {
                                                        e.printStackTrace();
                                                    } catch (IllegalStateException e) {
                                                        e.printStackTrace();
                                                    } catch (IOException e) {
                                                        e.printStackTrace();
                                                    }
                                                } else {
                                                    stopAnimation2();
                                                    releaseMp3Player();

                                                    preview_record_img2.setImageResource(R.drawable.preview_record_img);
                                                }
                                            }
                                        }
                                    });


                                }
                            }
                        }
                        mTitleEt.setText(mPainPointItem.name);
                    } else {
                        resetValue();
                        mTitleEt.setText("");
                    }
                }

                mCurrentSelectedEnum = CustomHabit;
                switchSelectState(mCurrentSelectedEnum);


                selectABHS = false;
                ViewGroup.LayoutParams pp3 = mContentLayout.getLayoutParams();
                pp3.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                mContentLayout.setLayoutParams(pp3);

                break;
            case R.id.notify_btn: // 通知提醒
                if (!isEdit) {
                    resetValue();
                    mTitleEt.setText("通知提醒");
                } else {
                    if (originalctype == 2) {
                        custom_channel.setVisibility(View.GONE);
                        mTitleEt.setText(mPainPointItem.name);
                    } else {
                        resetValue();
                        mTitleEt.setText("通知提醒");
                    }

                    releaseMp3Player();
                    stopAnimation();
                    stopAnimation2();
                }
                mCurrentSelectedEnum = CustomContent;
                tv_custom11111.setBackgroundResource(R.drawable.round_corner_with_blue_stroke);
                tv_record1111.setBackground(null);
                switchSelectState(mCurrentSelectedEnum);

                rel_abhs.setVisibility(View.GONE);
                lin_abhs.setVisibility(View.GONE);
                rel_yxkc.setVisibility(View.GONE);
                view_lllll.setVisibility(View.GONE);

                ViewGroup.LayoutParams pp4 = mContentLayout.getLayoutParams();
                pp4.height = Tools.getPixelByDip(this, 210);

                mContentLayout.setLayoutParams(pp4);

                selectABHS = false;
                break;
            case R.id.set_timer_tv:
            case R.id.set_timer_img:
                setHabitTime();
                break;
            case R.id.tv_custom11111:
                mCurrentSelectedEnum = CustomContent;
                tv_custom11111.setBackgroundResource(R.drawable.round_corner_with_blue_stroke);
                tv_record1111.setBackground(null);
                switchSelectState(mCurrentSelectedEnum);
                break;
            case R.id.tv_record1111:
                mCurrentSelectedEnum = Recording;
                tv_custom11111.setBackground(null);
                tv_record1111.setBackgroundResource(R.drawable.round_corner_with_blue_stroke);
                switchSelectState(mCurrentSelectedEnum);
                break;
            case R.id.preview_record_img:
            case R.id.preview_listen_caption_tv:
                if (Tools.isNotEmpty(mTtsContentEt.getText().toString())) {
                    if (mCurrentSelectedEnum == CustomContent) {
                        tryListenOnline = false;
                    } else {
                        // TODO
                    }
                }

                if (tryListenOnline) {
                    releaseMp3Player();
                    if (mp3Player == null)
                        mp3Player = new MediaPlayer();
                    try {
                        mp3Player.setDataSource(mPainPointItem.mp3_url);
                        mp3Player.prepare();
                        mp3Player.start();
                        startAnimation();
                        mp3Player.setOnCompletionListener(mediaPlayerListener);
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (SecurityException e) {
                        e.printStackTrace();
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    if (mCurrentSelectedEnum == CustomContent) {
                        String text = mTtsContentEt.getText().toString();
                        if (Tools.isEmpty(text)) {
                            showTip("内容不能为空");
                            return;
                        }

                        if (text.length() > 140) {
                            showTip("最多不超过140个字");
                            return;
                        }

                        // 设置参数
                        setParam();
                        if (mp3Player != null && mp3Player.isPlaying()) {
                            releaseMp3Player();
                        }
                        int code = mTts.startSpeaking(text, mTtsListener);

                        if (code != ErrorCode.SUCCESS) {
                            if (code == ErrorCode.ERROR_COMPONENT_NOT_INSTALLED) {
                                //未安装则跳转到提示安装页面
                            } else {
                                showTip("语音合成失败,错误码: " + code);
                            }
                        }
                    } else { //recording mode
                        releaseMp3Player();
                        previewRecordedAudio();
                    }
                }
                break;
            case R.id.save_and_upload_btn:
                uploadAudioFile(TTS_AUDIO_PATH.replace(".wav", ".mp3"));
                break;
            case R.id.record_state_btn:
                switch (RECORING_STATE) {
                    case PRE_RECORDING:
                        tryListenOnline = false;
                        RECORING_STATE = RecordingState.RECORDING;
                        mRecordStateBtn.setText("完成");

                        mRecorder = new MediaRecorder();

                        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                        mRecorder.setOutputFile(RECORDING_AUDIO_PATH);

                        MediaRecorder.OnErrorListener errorListener = null;
                        mRecorder.setOnErrorListener(errorListener);
                        MediaRecorder.OnInfoListener infoListener = null;
                        mRecorder.setOnInfoListener(infoListener);

                        try {
                            mRecorder.prepare();
                            mRecorder.start();
                            startCountdownTimer();
                            isRecording = true; // we are currently recording
                        } catch (IllegalStateException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        handler.post(updateVisualizer);
                        break;
                    case RECORDING:
                        RECORING_STATE = RecordingState.STOP_RECORDING;
                        mRecordStateBtn.setText("保存");
                        isRecording = false;
                        releaseRecorder();

                        new Thread() {
                            @Override
                            public void run() {
                                convert2Mp3(RECORDING_AUDIO_PATH);
                            }
                        }.start();

                        break;
                    case STOP_RECORDING:
                        RECORING_STATE = RecordingState.PRE_RECORDING;
                        mRecordStateBtn.setText("开始录音");
                        //  uploadAudioFile(RECORDING_AUDIO_PATH);

                        uploadAudioFile(RECORDING_AUDIO_PATH.replace(".amr", ".mp3"));
                        break;
                    default:
                        break;
                }
                break;
            case R.id.retry_record_tv:
                mRecordStateBtn.setText("开始录音");
                RECORING_STATE = RecordingState.PRE_RECORDING;
                isRecording = false;
                mRecordVisualizerView.clear();
                mRecordVisualizerView.postInvalidate();
                releaseRecorder();
                releaseMp3Player();
                break;
            default:
                break;
        }
    }

    private void convert2Mp3(String sourceFile) {
        if (Tools.isEmpty(sourceFile)) {
            //showTip("音频文件名为空！");
            return;
        }

        File wavFile = new File(sourceFile);
        if (!wavFile.exists()) {
            showTip("生成音频文件失败，请重新试听！");
            return;
        }
        IConvertCallback callback = new IConvertCallback() {
            @Override
            public void onSuccess(File convertedFile) {
                Toast.makeText(AddHabitClockActivity.this, "成功生成语音文件" + convertedFile.getPath(), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(Exception error) {
                // Toast.makeText(AddHabitClockActivity.this, "ERROR: " + error.getMessage(), Toast.LENGTH_LONG).show();
            }
        };
        //  Toast.makeText(this, "Converting audio file...", Toast.LENGTH_SHORT).show();
        AndroidAudioConverter.with(this)
                .setFile(wavFile)
                .setFormat(AudioFormat.MP3)
                .setCallback(callback)
                .convert();
    }

    private Runnable mRrrr = new Runnable() {
        @Override
        public void run() {

        }
    };


    /**
     * 上传非通知提醒的方法
     */
    private void submitMultiCustomHabit() {
        releaseMp3Player();
        stopAnimation2();
        if (!imgList1.isEmpty()) {
            for (ImageView item : imgList1) {
                item.setImageResource(R.drawable.preview_record_img);
            }
        }
        if (!imgList2.isEmpty()) {
            for (ImageView item : imgList2) {
                item.setImageResource(R.drawable.preview_record_img);
            }
        }

        if (!aiList.isEmpty()) {   // 1:大众课程 3：习惯养成
            for (int i = 0; i < aiList.size(); i++) {
                final int j = i;
                final AlbumItem iiitem = aiList.get(i);
                new CustomAsyncTask(this, new IAsyncTask() {
                    @Override
                    public void onRecieveData(Activity activity, ResponseResult result) {
                        if (j == aiList.size() - 1) {
                            dismissLoading();
                            if (result.isSuccess()) {
                                toastShow("提交成功");
                                Intent resultIntent = new Intent();
                                setResult(RESULT_OK, resultIntent);
                                Intent intent = new Intent("updatehabit");
                                sendBroadcast(intent);
                                finish();
                            } else {
                                dismissLoading();
                                toastShow(result.data);
                            }
                        }
                    }

                    @Override
                    public ResponseResult doInbackground(Activity activity) {
                        HashMap<String, Object> param_ = new HashMap<String, Object>();
                        param_.put(HttpUtils.TAG_SNO_I, leUser.sno);
                        if (Tools.isNotEmpty(selectQueryDate)) {
                            param_.put("date", selectQueryDate);
                        }
                        if (Tools.isNotEmpty(iiitem.url)) {
                            param_.put("mp3_url", iiitem.url);
                        }
                        param_.put("ctype", ctype);
                        if (ctype == 1 && iiitem != null) {
                            if (Tools.isNotEmpty(iiitem.album_cover)) {
                                param_.put("info_img", iiitem.album_cover);
                            }
                            param_.put("info_title", iiitem.album_name + " 第" + iiitem.sort + "集" + " (" + Tools.secToTime(iiitem.length) + ")");
                            param_.put("info_desc", iiitem.title);
                            if (selectABHS) {
                                param_.put("emc", 1);
                            } else {
                                param_.put("emc", 0);
                            }
                        } else if (ctype == 3 && iiitem != null) {
                            if (Tools.isNotEmpty(iiitem.album_cover)) {
                                param_.put("info_img", iiitem.album_cover);
                            }
                            param_.put("info_title", iiitem.album_name + " 第" + iiitem.sort + "集" + " (" + Tools.secToTime(iiitem.length) + ")");
                            param_.put("info_desc", iiitem.title);

                            if (selectABHS) {
                                param_.put("emc", 1);
                            } else {
                                param_.put("emc", 0);
                            }
                        }

                        if (Tools.isNotEmpty(mTitleEt.getText().toString().trim())) {
                            param_.put("name", mTitleEt.getText().toString().trim());
                        }

                        if (Tools.isNotEmpty(mSetTimerTv.getText().toString().trim())) {
                            param_.put("time", mSetTimerTv.getText().toString().trim());
                        }

                        if (j == aiList.size() - 1) {
                            param_.put("is_send", 1); // TODO 最后一个提交的要传
                        }

                        if (Tools.isNotEmpty(beginDate) && Tools.isNotEmpty(endDate)) {
                            param_.put("date_from", beginDate);
                            param_.put("date_to", endDate);
                        }

                        param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);
                        param_.put("class_num", classNum);
                        return HttpUtils.startRequest(HttpUtils.API_URL + "/api/2.0/operation/habit/habits_teacher",
                                param_, HttpUtils.TAG_POST_REQUEST);

                    }
                }).execute("请检查网络连接!");
            }
        } else if (!caiList.isEmpty()) {
            for (int i = 0; i < caiList.size(); i++) {
                final int j = i;
                final CustomAlbumItem iiitem = caiList.get(i);
                new CustomAsyncTask(this, new IAsyncTask() {
                    @Override
                    public void onRecieveData(Activity activity, ResponseResult result) {
                        if (j == caiList.size() - 1) {
                            dismissLoading();
                            if (result.isSuccess()) {
                                toastShow("提交成功");
                                Intent resultIntent = new Intent();
                                setResult(RESULT_OK, resultIntent);
                                Intent intent = new Intent("updatehabit");
                                sendBroadcast(intent);
                                finish();
                            } else {
                                dismissLoading();
                                toastShow(result.data);
                            }
                        }
                    }

                    @Override
                    public ResponseResult doInbackground(Activity activity) {
                        HashMap<String, Object> param_ = new HashMap<String, Object>();
                        param_.put(HttpUtils.TAG_SNO_I, leUser.sno);
                        if (Tools.isNotEmpty(selectQueryDate)) {
                            param_.put("date", selectQueryDate);
                        }

                        if (Tools.isNotEmpty(iiitem.url)) {
                            param_.put("mp3_url", iiitem.url);
                        }

                        param_.put("ctype", ctype);

                        if (ctype == 0 && iiitem != null) {
                            if (Tools.isNotEmpty(iiitem.album_cover)) {
                                param_.put("info_img", iiitem.album_cover);
                            }
                            param_.put("info_title", iiitem.album_name + " 第" + iiitem.sort + "集" + " (" + Tools.secToTime(iiitem.length) + ")");
                            param_.put("info_desc", iiitem.title);
                            if (selectABHS) {
                                param_.put("emc", 1);
                            } else {
                                param_.put("emc", 0);
                            }
                        }

                        if (Tools.isNotEmpty(mTitleEt.getText().toString().trim())) {
                            param_.put("name", mTitleEt.getText().toString().trim());
                        }

                        if (Tools.isNotEmpty(mSetTimerTv.getText().toString().trim())) {
                            param_.put("time", mSetTimerTv.getText().toString().trim());
                        }

                        if (j == caiList.size() - 1) {
                            param_.put("is_send", 1); // TODO 最后一个提交的要传
                        }

                        if (Tools.isNotEmpty(beginDate) && Tools.isNotEmpty(endDate)) {
                            param_.put("date_from", beginDate);
                            param_.put("date_to", endDate);
                        }

                        param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);
                        param_.put("class_num", classNum);
                        return HttpUtils.startRequest(HttpUtils.API_URL + "/api/2.0/operation/habit/habits_teacher", param_, HttpUtils.TAG_POST_REQUEST);

                    }
                }).execute("请检查网络连接!");
            }
        }
    }

    /**
     * 提交或者编辑添加课程
     */
    private void publishCustomHabit() {
        // --------------- 判断是否符合提交或编辑条件-----------------
        if (Tools.isEmpty(mTitleEt.getText().toString())) {
            showTip("您还没有填写习惯标题!");
            return;
        }

        if (!mSetTimerTv.getText().toString().contains(":")) {
            showTip("您还没有设置时间!");
            return;
        }

        switch (mCurrentSelectedEnum) {
            case CustomContent:
            case Recording:
                if (!isEdit && Tools.isEmpty(mp3url_toSubmit)) {   // 通知提醒
                    showTip("请上传音频文件");
                    return;
                }
                break;
            case CustomClass: // 课程课件
            case CommonClass: // 大众课程
            case CustomHabit: // 习惯定制
                if (!isEdit && aiList.isEmpty() && caiList.isEmpty()) {  // 可多选的栏目
                    showTip("请选择音频文件");
                    return;
                }
                break;
        }


        if (isEdit) { // -------------编辑模式-----------------
            if (ctype == 2) {
                if (Tools.isEmpty(mp3url_toSubmit)) {
                    String audioFilePath = "";
                    if (mCurrentSelectedEnum == CustomContent) {
                        audioFilePath = TTS_AUDIO_PATH; // TTS
                        int ret = uploadAudioFile(TTS_AUDIO_PATH.replace(".wav", ".mp3"));
                        if (ret == 1) {
                            modifyCustomHabit(mTitleEt.getText().toString(), mSetTimerTv.getText().toString());
                        }
                    } else if (mCurrentSelectedEnum == Recording) {
                        audioFilePath = RECORDING_AUDIO_PATH; // 录音
                        uploadAudioFile(RECORDING_AUDIO_PATH.replace(".amr", ".mp3"));
                    }
                } else {
                    modifyCustomHabit(mTitleEt.getText().toString(), mSetTimerTv.getText().toString());
                }
            } else {
                modifyCustomHabit(mTitleEt.getText().toString(), mSetTimerTv.getText().toString());
            }
        } else { // -------------提交模式-----------------
            if (ctype == 2) { // 通知提醒
                if (Tools.isEmpty(mp3url_toSubmit)) {
                    if (mCurrentSelectedEnum == CustomContent) {
                        uploadAudioFile(TTS_AUDIO_PATH.replace(".wav", ".mp3"));
                    } else if (mCurrentSelectedEnum == Recording) {
                        uploadAudioFile(RECORDING_AUDIO_PATH.replace(".amr", ".mp3"));
                    }
                } else {
                    submitCustomHabit();
                }
            } else {  // 非通知提醒
                submitMultiCustomHabit();
            }
        }
    }

    /**
     * 修改习惯
     *
     * @param title
     * @param time
     */
    private void modifyCustomHabit(final String title, final String time) {
        releaseMp3Player();
        stopAnimation2();
        if (!imgList1.isEmpty()) {
            for (ImageView item : imgList1) {
                item.setImageResource(R.drawable.preview_record_img);
            }
        }
        if (!imgList2.isEmpty()) {
            for (ImageView item : imgList2) {
                item.setImageResource(R.drawable.preview_record_img);
            }
        }

        new CustomAsyncTask(this, new IAsyncTask() {
            @Override
            public void onRecieveData(Activity activity, ResponseResult result) {
                dismissLoading();
                if (result.isSuccess()) {
                    toastShow("修改成功");
                    Intent resultIntent = new Intent();
                    setResult(RESULT_OK, resultIntent);

                    Intent intent = new Intent("updatehabit");
                    sendBroadcast(intent);

                    finish();
                } else {
                    dismissLoading();
                    toastShow(result.data);
                }
            }

            @Override
            public ResponseResult doInbackground(Activity activity) {
                HashMap<String, Object> param_ = new HashMap<String, Object>();
                param_.put(HttpUtils.TAG_SNO_I, leUser.sno);

                if (Tools.isNotEmpty(selectQueryDate)) {
                    param_.put("date", selectQueryDate);
                }

                if (ctype == 2) {
                    if (Tools.isNotEmpty(mp3url_toSubmit)) {
                        param_.put("mp3_url", mp3url_toSubmit);
                    }
                } else {
                    if (!aiList.isEmpty()) {
                        param_.put("mp3_url", aiList.get(0).url);
                        if (Tools.isNotNullStr(aiList.get(0).album_cover))
                            param_.put("info_img", aiList.get(0).album_cover);
                        param_.put("info_title", aiList.get(0).album_name + " 第" + aiList.get(0).sort + "集" + " (" + Tools.secToTime(aiList.get(0).length) + ")");
                        param_.put("info_desc", aiList.get(0).title);
                    } else if (!caiList.isEmpty()) {
                        param_.put("mp3_url", caiList.get(0).url);
                        if (Tools.isNotNullStr(caiList.get(0).album_cover))
                            param_.put("info_img", caiList.get(0).album_cover);
                        param_.put("info_title", caiList.get(0).album_name + " 第" + caiList.get(0).sort + "集" + " (" + Tools.secToTime(caiList.get(0).length) + ")");
                        param_.put("info_desc", caiList.get(0).title);
                    }
                }

                param_.put("ctype", ctype);

                if (ctype == 1 || ctype == 0 || ctype == 3) {
                    if (selectABHS) {
                        param_.put("emc", 1);
                    } else {
                        param_.put("emc", 0);
                    }
                }

                param_.put("name", title);
                param_.put("time", time);
                param_.put("class_num", classNum);
                param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);
                return HttpUtils.startRequest(HttpUtils.API_URL + "/api/2.0/operation/habit/habits_teacher/" + mPainPointItem.id, param_, HttpUtils.TAG_PUT_REQUEST);

            }
        }).execute("请检查网络连接!");
    }


    /**
     * 上传音频文件
     *
     * @param filePath
     * @return
     */
    private int uploadAudioFile(final String filePath) {
        if (Tools.isEmpty(filePath)) {
            return 0;
        }
        File file = new File(filePath);
        if (!file.exists()) {
            if (isEdit) {
                return 1;
            } else {
                showTip("请先进行试听，生成音频文件!");
            }

            return 0;
        }

        showloading("");

        new CustomAsyncTask(this, new IAsyncTask() {
            @Override
            public void onRecieveData(Activity activity, ResponseResult result) {
                if (result.isSuccess()) {
                    mp3url_toSubmit = result.data;
                    toastShow("上传成功");
                } else {
                    toastShow(result.data);
                }
                dismissLoading();


            }

            @Override
            public ResponseResult doInbackground(Activity activity) {

                HashMap<String, Object> param_ = new HashMap<String, Object>();
                param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);
                param_.put(HttpUtils.TAG_MOBILE_I, leUser.id);
                param_.put(HttpUtils.TAG_MODULE_I, "custom_habits/audio");
                return HttpUtils.startUploadRequest(HttpUtils.API_URL
                                + HttpUtils.UPLOAD_BABY_AVATAR_POST, param_, "Filedata", filePath,
                        HttpUtils.TAG_POST_REQUEST);

            }
        }).execute("请检查网络连接!");
        return 0;
    }


    private String mp3url_Online, mp3url_toSubmit;

    /**
     * 提交通知提醒
     */
    private void submitCustomHabit() {
        releaseMp3Player();
        stopAnimation2();

        if (!imgList1.isEmpty()) {
            for (ImageView item : imgList1) {
                item.setImageResource(R.drawable.preview_record_img);
            }
        }

        if (!imgList2.isEmpty()) {
            for (ImageView item : imgList2) {
                item.setImageResource(R.drawable.preview_record_img);
            }
        }

        new CustomAsyncTask(this, new IAsyncTask() {
            @Override
            public void onRecieveData(Activity activity, ResponseResult result) {
                dismissLoading();
                if (result.isSuccess()) {
                    toastShow("提交成功");
                    Intent resultIntent = new Intent();
                    setResult(RESULT_OK, resultIntent);
                    Intent intent = new Intent("updatehabit");
                    sendBroadcast(intent);
                    finish();
                } else {
                    dismissLoading();
                    toastShow(result.data);
                }
            }

            @Override
            public ResponseResult doInbackground(Activity activity) {
                HashMap<String, Object> param_ = new HashMap<String, Object>();
                param_.put(HttpUtils.TAG_SNO_I, leUser.sno);
                if (Tools.isNotEmpty(selectQueryDate)) {
                    param_.put("date", selectQueryDate);
                }
                if (Tools.isNotEmpty(mp3url_toSubmit)) {
                    param_.put("mp3_url", mp3url_toSubmit);
                }
                param_.put("ctype", ctype);
                if (Tools.isNotEmpty(mTitleEt.getText().toString().trim())) {
                    param_.put("name", mTitleEt.getText().toString().trim());
                }
                if (Tools.isNotEmpty(mSetTimerTv.getText().toString().trim())) {
                    param_.put("time", mSetTimerTv.getText().toString().trim());
                }
                param_.put("is_send", 1); // TODO 最后一个提交的要传
                if (Tools.isNotEmpty(beginDate) && Tools.isNotEmpty(endDate)) {
                    param_.put("date_from", beginDate);
                    param_.put("date_to", endDate);
                }
                param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);
                param_.put("class_num", classNum);
                return HttpUtils.startRequest(HttpUtils.API_URL + "/api/2.0/operation/habit/habits_teacher",
                        param_, HttpUtils.TAG_POST_REQUEST);
            }
        }).execute("请检查网络连接!");
    }


    private MediaPlayer.OnCompletionListener mediaPlayerListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            stopAnimation();
        }
    };


    private MediaPlayer.OnCompletionListener mediaPlayerListener2 = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            stopAnimation2();
        }
    };

    private void previewRecordedAudio() {
        if (mTts != null && mTts.isSpeaking()) {
            mTts.stopSpeaking();
        }
        if (mp3Player == null)
            mp3Player = new MediaPlayer();
        try {
            mp3Player.setDataSource(RECORDING_AUDIO_PATH);
            mp3Player.prepare();
            mp3Player.start();
            startAnimation();
            mp3Player.setOnCompletionListener(mediaPlayerListener);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void releaseMp3Player() {
        if (mp3Player != null) {
            mp3Player.stop();
            mp3Player.setOnCompletionListener(null);
            mp3Player.release();
            mp3Player = null;
        }
    }

    Runnable updateVisualizer = new Runnable() {
        @Override
        public void run() {
            if (isRecording) {
                int x = mRecorder.getMaxAmplitude();
                mRecordVisualizerView.addAmplitude(x);
                mRecordVisualizerView.invalidate();

                handler.postDelayed(this, REPEAT_INTERVAL);
            }
        }
    };

    /**
     * 初始化监听。
     */
    private InitListener mTtsInitListener = new InitListener() {
        @Override
        public void onInit(int code) {
            Log.d(TAG, "InitListener init() code = " + code);
            if (code != ErrorCode.SUCCESS) {
                showTip("初始化失败,错误码：" + code);
            } else {
                // 初始化成功，之后可以调用startSpeaking方法
                // 注：有的开发者在onCreate方法中创建完合成对象之后马上就调用startSpeaking进行合成，
                // 正确的做法是将onCreate中的startSpeaking调用移至这里
            }
        }
    };


    /**
     * 参数设置
     *
     * @return
     */
    private void setParam() {
        // 清空参数
        mTts.setParameter(SpeechConstant.PARAMS, null);
        // 根据合成引擎设置相应参数
        if (mEngineType.equals(SpeechConstant.TYPE_CLOUD)) {
            mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
            // 设置在线合成发音人
            mTts.setParameter(SpeechConstant.VOICE_NAME, voicer);
            //设置合成语速
            mTts.setParameter(SpeechConstant.SPEED, "50");
            //设置合成音调
            mTts.setParameter(SpeechConstant.PITCH, "50");
            //设置合成音量
            mTts.setParameter(SpeechConstant.VOLUME, "50");
        } else {
            mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_LOCAL);
            // 设置本地合成发音人 voicer为空，默认通过语记界面指定发音人。
            mTts.setParameter(SpeechConstant.VOICE_NAME, "");
            /**
             * TODO 本地合成不设置语速、音调、音量，默认使用语记设置
             * 开发者如需自定义参数，请参考在线合成参数设置
             */
        }
        //设置播放器音频流类型
        mTts.setParameter(SpeechConstant.STREAM_TYPE, "3");
        // 设置播放合成音频打断音乐播放，默认为true
        mTts.setParameter(SpeechConstant.KEY_REQUEST_FOCUS, "true");

        // 设置音频保存路径，保存音频格式支持pcm、wav，设置路径为sd卡请注意WRITE_EXTERNAL_STORAGE权限
        // 注：AUDIO_FORMAT参数语记需要更新版本才能生效
        mTts.setParameter(SpeechConstant.AUDIO_FORMAT, "wav");
        mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, TTS_AUDIO_PATH);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        releaseRecorder();
        releaseMp3Player();

        if (aiList != null) {
            aiList.clear();
            aiList = null;
        }

        if (caiList != null) {
            caiList.clear();
            caiList = null;
        }

        if (null != mTts) {
            mTts.stopSpeaking();
            // 退出时释放连接
            mTts.destroy();
        }
    }

    private void releaseRecorder() {
        stopCountdownTimer();
        if (mRecorder != null) {
            isRecording = false; // stop recording
            handler.removeCallbacks(updateVisualizer);
            mRecordVisualizerView.clear();
            mRecorder.stop();
            mRecorder.reset();
            mRecorder.release();
            mRecorder = null;
        }
    }

    /**
     * 合成回调监听。
     */
    private SynthesizerListener mTtsListener = new SynthesizerListener() {

        @Override
        public void onSpeakBegin() {
            //showTip("开始播放");
            startAnimation();
        }

        @Override
        public void onSpeakPaused() {
            showTip("暂停播放");
            stopAnimation();
        }

        @Override
        public void onSpeakResumed() {
            showTip("继续播放");
            startAnimation();
        }

        @Override
        public void onBufferProgress(int percent, int beginPos, int endPos,
                                     String info) {
            // 合成进度
            mPercentForBuffering = percent;
            showTip(String.format("正在合成语音%d%%",
                    mPercentForBuffering));
        }

        @Override
        public void onSpeakProgress(int percent, int beginPos, int endPos) {
            // 播放进度
            mPercentForPlaying = percent;
//            showTip(String.format(getString(R.string.tts_toast_format),
//                    mPercentForBuffering, mPercentForPlaying));
        }

        @Override
        public void onCompleted(SpeechError error) {
            if (error == null) {
//                showTip("已完成语音合成");
                convert2Mp3(TTS_AUDIO_PATH);
            } else if (error != null) {
                showTip(error.getPlainDescription(true));
            }
            stopAnimation();
        }

        @Override
        public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {
            // 以下代码用于获取与云端的会话id，当业务出错时将会话id提供给技术支持人员，可用于查询会话日志，定位出错原因
            // 若使用本地能力，会话id为null
            //	if (SpeechEvent.EVENT_SESSION_ID == eventType) {
            //		String sid = obj.getString(SpeechEvent.KEY_EVENT_SESSION_ID);
            //		Log.d(TAG, "session id =" + sid);
            //	}
        }
    };

    private void showTip(final String str) {
        mToast.setText(str);
        mToast.show();
    }

    private void setHabitTime() {
        CustomTimeDialog.Builder customBuilder = new CustomTimeDialog.Builder(
                this);
        customBuilder.setTitle("设置每次启动时间").setDateStr(selectQueryDate)
                .setHourMinuteStr(mSetTimerTv.getText().toString())
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setPositiveButton("确定", new CustomTimeDialog.Builder.OnOkClickListener() {
            @Override
            public void onClickOk(final String strDate, final int hourOfDay, final int minute) {

                String text = String.format("%02d", hourOfDay) + ":"
                        + String.format("%02d", minute);

                selectQueryDate = strDate;
                mSetTimerTv.setText(text);


                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//                mCurDay = sdf.format(Calendar.getInstance().getTime());


                mCurDay = selectQueryDate;
//                Calendar bef = Calendar.getInstance();
//                bef.setTime(sdf.parse(lu.machine.dob));


                try {
                    Date date2 = Tools.getDateAfter(sdf.parse(selectQueryDate), 1);
                    Date date3 = Tools.getDateAfter(sdf.parse(selectQueryDate), 3);
                    Date date4 = Tools.getDateAfter(sdf.parse(selectQueryDate), 6);
                    Date date5 = Tools.getDateAfter(sdf.parse(selectQueryDate), 13);
                    Date date6 = Tools.getDateAfter(sdf.parse(selectQueryDate), 27);
                    Date date7 = Tools.getDateAfter(sdf.parse(selectQueryDate), 59);

                    mCurDay2 = sdf.format(date2);
                    mCurDay3 = sdf.format(date3);
                    mCurDay4 = sdf.format(date4);
                    mCurDay5 = sdf.format(date5);
                    mCurDay6 = sdf.format(date6);
                    mCurDay7 = sdf.format(date7);

                } catch (ParseException e) {
                    e.printStackTrace();
                }


                if (lin_abhs.getVisibility() == View.VISIBLE) {
                    tv_cv1.setText("1. " + mCurDay + "   " + mSetTimerTv.getText().toString());
                    tv_cv2.setText("2. " + mCurDay2 + "   " + mSetTimerTv.getText().toString());
                    tv_cv3.setText("3. " + mCurDay3 + "   " + mSetTimerTv.getText().toString());
                    tv_cv4.setText("4. " + mCurDay4 + "   " + mSetTimerTv.getText().toString());
                    tv_cv5.setText("5. " + mCurDay5 + "   " + mSetTimerTv.getText().toString());
                    tv_cv6.setText("6. " + mCurDay6 + "   " + mSetTimerTv.getText().toString());
                    tv_cv7.setText("7. " + mCurDay7 + "   " + mSetTimerTv.getText().toString());
                }

                timeDlg.dismiss();

            }

        });

        if (!this.isFinishing()) {
            timeDlg = customBuilder.create();
            timeDlg.show();
        }
    }
}
